Skip to content

Commit 7ff5562

Browse files
update java demo to v25.1
update java demo to support select local file
1 parent 7310609 commit 7ff5562

File tree

9 files changed

+267
-38
lines changed

9 files changed

+267
-38
lines changed

Examples.GridJs/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ EXPOSE 8080
4646
# COPY fonts/* /usr/share/fonts/
4747
RUN mkdir -p /app/wb
4848
RUN mkdir -p /app/grid_cache/streamcache
49+
RUN mkdir -p /app/upload
4950
COPY wb/*.xlsx /app/wb/
5051
# RUN ls -l /app/
5152
# 启动应用

Examples.GridJs/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
<dependency>
5252
<groupId>com.aspose</groupId>
5353
<artifactId>aspose-cells</artifactId>
54-
<version>24.12</version>
54+
<version>25.1</version>
5555
</dependency>
5656
<dependency>
5757
<groupId>com.aspose</groupId>
5858
<artifactId>aspose-cells</artifactId>
59-
<version>24.12</version>
59+
<version>25.1</version>
6060
<classifier>gridjs</classifier>
6161
</dependency>
6262
<!-- <dependency>

Examples.GridJs/src/main/java/com/aspose/gridjs/demo/GridjsdemoApplication.java

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,42 @@
33
import org.springframework.beans.factory.annotation.Value;
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.context.ApplicationContext;
68

79
import com.aspose.cells.Workbook;
810
import com.aspose.gridjs.Config;
911
import com.aspose.gridjs.GridJsWorkbook;
1012

1113
import javax.annotation.PostConstruct;
1214

15+
16+
@Component
17+
class MyConfig {
18+
19+
@Value("${testconfig.CachePath}")
20+
public String cachePath;
21+
22+
23+
@Value("${testconfig.AsposeLicensePath}")
24+
public String asposeLicensePath;
25+
26+
27+
}
28+
1329
@SpringBootApplication
1430
public class GridjsdemoApplication {
15-
private static String cachePath;
31+
32+
1633

17-
@Value("${testconfig.CachePath}")
18-
private String cachePathProperty;
1934

20-
@PostConstruct
21-
private void init() {
22-
cachePath = this.cachePathProperty;
35+
//settings for GridJs,implement GridCacheForStream to store cache file
36+
private static void init(MyConfig myConfig) {
37+
38+
39+
2340
try {
24-
Config.setFileCacheDirectory(cachePath);
41+
Config.setFileCacheDirectory(myConfig.cachePath);
2542
//lazy loading
2643
Config.setLazyLoading(true);
2744
} catch (Exception e) {
@@ -36,24 +53,39 @@ private void init() {
3653
GridJsWorkbook.UpdateMonitor = new ModifyMonitor();
3754
}
3855

39-
//simple way not use GridJsWorkbook.CacheImp ,shall also ok
40-
private void init2() {
41-
cachePath = this.cachePathProperty;
56+
//settings for GridJs,just set a temp path to store cache file
57+
//simple way not use GridJsWorkbook.CacheImp ,it shall also ok
58+
private static void init2(MyConfig myConfig) {
59+
4260
try {
43-
Config.setFileCacheDirectory(cachePath);
61+
Config.setFileCacheDirectory(myConfig.cachePath);
62+
//lazy loading
63+
Config.setLazyLoading(true);
4464

4565
} catch (Exception e) {
4666
e.printStackTrace();
4767
}
48-
//need to setImageUrlBase
49-
GridJsWorkbook.setImageUrlBase("/GridJs2/Image");
5068

51-
69+
//need to setImageUrlBase in this case
70+
GridJsWorkbook.setImageUrlBase("/GridJs2/Image");
71+
GridJsWorkbook.UpdateMonitor = new ModifyMonitor();
72+
5273
}
74+
5375

5476
public static void main(String[] args) {
77+
78+
79+
80+
ApplicationContext context = SpringApplication.run(GridjsdemoApplication.class, args);
5581

56-
SpringApplication.run(GridjsdemoApplication.class, args);
82+
MyConfig myConfig = context.getBean(MyConfig.class);
83+
84+
//set license for aspose cells
85+
com.aspose.cells.License lic=new com.aspose.cells.License();
86+
//lic.setLicense(myConfig.asposeLicensePath);
87+
init(myConfig);
88+
5789
}
5890

5991
}

Examples.GridJs/src/main/java/com/aspose/gridjs/demo/LocalFileCache.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.aspose.gridjs.demo;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.File;
36
import java.io.FileInputStream;
47
import java.io.FileNotFoundException;
58
import java.io.FileOutputStream;
@@ -8,15 +11,33 @@
811
import java.nio.file.Files;
912
import java.nio.file.Paths;
1013

14+
import com.aspose.cells.SaveFormat;
15+
import com.aspose.cells.Workbook;
1116
import com.aspose.gridjs.Config;
1217
import com.aspose.gridjs.GridCacheForStream;
1318

1419
public class LocalFileCache extends GridCacheForStream {
15-
20+
21+
private String streamcacheDir=null;
22+
//default constructor
23+
public LocalFileCache() {
24+
//make sure the cache path existed
25+
streamcacheDir=Paths.get(Config.getFileCacheDirectory(), "streamcache").toString();
26+
File dir = new File(streamcacheDir);
27+
if (!dir.exists()) {
28+
try {
29+
Files.createDirectories(Paths.get(streamcacheDir));
30+
} catch (IOException e) {
31+
// TODO Auto-generated catch block
32+
e.printStackTrace();
33+
}
34+
}
35+
}
36+
1637
@Override
1738
public void saveStream(InputStream s, String uid) {
18-
// make sure the directory is exist
19-
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
39+
// save stream to the cache path with uid
40+
String filepath = Paths.get(streamcacheDir, uid.replace('/', '.')).toString();
2041
try (FileOutputStream fos = new FileOutputStream(filepath.toString())) {
2142
s.reset(); // Equivalent to s.Position = 0 in C#
2243
byte[] buffer = new byte[1024];
@@ -33,8 +54,21 @@ public void saveStream(InputStream s, String uid) {
3354

3455
@Override
3556
public InputStream loadStream(String uid) {
36-
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
57+
//load file from the cache path by uid
58+
String filepath = Paths.get(streamcacheDir, uid.replace('/', '.')).toString();
3759
try {
60+
//here you can update the stream per your business logic if you wish
61+
//for example below we modify the cell a1 value to hello
62+
/*
63+
Workbook wb = new Workbook(filepath);
64+
wb.getWorksheets().get(0).getCells().get("A1").putValue("hello");
65+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
66+
wb.save(bos, SaveFormat.XLSX);
67+
byte[] byteArray = bos.toByteArray();
68+
bos.close();
69+
ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
70+
return bis;
71+
*/
3872
return new FileInputStream(filepath);
3973
} catch (FileNotFoundException e) {
4074
e.printStackTrace();
@@ -44,10 +78,12 @@ public InputStream loadStream(String uid) {
4478

4579
@Override
4680
public boolean isExisted(String uid) {
47-
String filepath = Paths.get(Config.getFileCacheDirectory(), "streamcache", uid.replace('/', '.')).toString();
81+
//check if the file with uid existed in the cache path
82+
String filepath = Paths.get(streamcacheDir, uid.replace('/', '.')).toString();
4883
return Files.exists(Paths.get(filepath));
4984
}
5085

86+
//return the action Url to get the file from the cache path by uid
5187
@Override
5288
public String getFileUrl(String uid) {
5389
return "/GridJs2/GetFileUseCacheStream?id=" + uid;

Examples.GridJs/src/main/java/com/aspose/gridjs/demo/controller/DataController.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
package com.aspose.gridjs.demo.controller;
22

33
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
49
import java.util.ArrayList;
510
import java.util.List;
611

712
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.http.ResponseEntity;
814
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.PostMapping;
916
import org.springframework.web.bind.annotation.RequestMapping;
1017
import org.springframework.web.bind.annotation.RequestParam;
1118
import org.springframework.web.bind.annotation.RestController;
19+
import org.springframework.web.multipart.MultipartFile;
1220
import org.springframework.web.servlet.ModelAndView;
1321

22+
1423
import com.aspose.gridjs.GridJsWorkbook;
1524

1625
@RestController
@@ -71,12 +80,49 @@ public ModelAndView uidtml(@RequestParam String filename,@RequestParam String ui
7180

7281

7382
ModelAndView mv = new ModelAndView();
74-
mv.addObject("uid", uid);
83+
mv.addObject("uid", uid);
7584
mv.addObject("file", filename);
76-
mv.setViewName("index");
85+
mv.setViewName("index");
7786
return mv;
7887
}
7988

89+
@Value("${testconfig.UploadPath}")
90+
private String uploadDir;
91+
92+
@PostMapping("/upload")
93+
public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) {
94+
// 检查文件是否为空
95+
if (file.isEmpty()) {
96+
return null;
97+
}
98+
99+
try {
100+
File dir = new File(uploadDir);
101+
if (!dir.exists()) {
102+
Files.createDirectories(Paths.get(uploadDir));
103+
}
104+
105+
Path filePath = Paths.get(uploadDir, file.getOriginalFilename());
106+
107+
file.transferTo(new File(filePath.toString()));
108+
109+
String filename=file.getOriginalFilename();
110+
111+
//get a unique id for the file
112+
String uid = GridJsWorkbook.getUidForFile(filename);
113+
114+
ModelAndView mv = new ModelAndView();
115+
mv.addObject("uid", uid);
116+
mv.addObject("file", filename);
117+
mv.setViewName("fileFromUpload");
118+
return mv;
119+
120+
} catch (Exception e) {
121+
e.printStackTrace();
122+
return null;
123+
}
124+
}
125+
80126

81127
}
82128

Examples.GridJs/src/main/java/com/aspose/gridjs/demo/controller/GridJs2Controller.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
5-
import java.io.File;
5+
import java.io.FileInputStream;
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.net.URL;
@@ -30,6 +30,7 @@
3030
import org.springframework.web.servlet.ModelAndView;
3131
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
3232

33+
import com.aspose.cells.LoadFormat;
3334
import com.aspose.cells.Workbook;
3435
import com.aspose.gridjs.Config;
3536
import com.aspose.gridjs.GridCellException;
@@ -46,7 +47,8 @@ public class GridJs2Controller {
4647
@Value("${testconfig.ListDir}")
4748
private String listDir;//="D:\\codebase\\customerissue\\wb\\tempfromdownload\\";
4849

49-
50+
51+
5052
@GetMapping("/DetailFileJsonWithUid")
5153
public ResponseEntity<String> detailFileJsonWithUid(@RequestParam String filename, @RequestParam String uid) {
5254
try {
@@ -83,8 +85,38 @@ public void detailStreamJsonWithUid(@RequestParam String filename, @RequestParam
8385
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(response.getOutputStream())) {
8486
boolean isdone= wbj.jsonToStreamByUid(gzipOutputStream,uid, filename);
8587
if (!isdone) {
86-
wbj.importExcelFile(uid,filePath.toString());
87-
wbj.jsonToStream(gzipOutputStream, filename);
88+
Workbook wb = new Workbook(filePath.toString());
89+
wbj.importExcelFile(uid,wb);
90+
wbj.jsonToStream(gzipOutputStream, filename);
91+
}
92+
} catch (IOException e) {
93+
// TODO Auto-generated catch block
94+
e.printStackTrace();
95+
} catch (Exception e) {
96+
// TODO Auto-generated catch block
97+
e.printStackTrace();
98+
}
99+
}
100+
101+
102+
@Value("${testconfig.UploadPath}")
103+
private String uploadDir;
104+
105+
@GetMapping("/DetailStreamJsonWithUidFromUpload")
106+
public void detailStreamJsonWithUidFromUpload(@RequestParam String filename, @RequestParam String uid,HttpServletResponse response) {
107+
108+
109+
Path filePath = Paths.get(uploadDir, filename);
110+
GridJsWorkbook wbj = new GridJsWorkbook();
111+
112+
response.setContentType("application/json");
113+
response.setHeader("Content-Encoding", "gzip");
114+
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(response.getOutputStream())) {
115+
boolean isdone= wbj.jsonToStreamByUid(gzipOutputStream,uid, filename);
116+
if (!isdone) {
117+
Workbook wb = new Workbook(filePath.toString());
118+
wbj.importExcelFile(uid,wb);
119+
wbj.jsonToStream(gzipOutputStream, filename);
88120
}
89121
} catch (IOException e) {
90122
// TODO Auto-generated catch block
@@ -103,7 +135,7 @@ public void lazyLoadingStreamJson(
103135

104136
GridJsWorkbook wbj = new GridJsWorkbook();
105137

106-
// ÉèÖÃÏìӦͷ
138+
// 设置响应头
107139
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
108140
response.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
109141

Examples.GridJs/src/main/resources/application.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ spring.servlet.multipart.max-request-size=10MB
55
#testconfig.ListDir=D:\\codebase\\customerissue\\wb\\tempfromdownload\\
66
#for linux
77
testconfig.ListDir=/app/wb
8-
#testconfig.FileName=controls.xlsx
98
testconfig.FileName=chart.xlsx
10-
#testconfig.CachePath=D:\\tmpdel\\tmp\\gridjsjava
11-
#for linux
129
testconfig.CachePath=/app/grid_cache
10+
testconfig.UploadPath=/app/upload
11+
testconfig.AsposeLicensePath=/app/aspose.lic
12+
1313

0 commit comments

Comments
 (0)