Skip to content

Commit ae3708a

Browse files
committed
springboot下载模板
1 parent 177d259 commit ae3708a

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,28 @@
5050
<groupId>org.springframework.boot</groupId>
5151
<artifactId>spring-boot-maven-plugin</artifactId>
5252
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-resources-plugin</artifactId>
56+
<configuration>
57+
<!-- 过滤不编译文件类型,解决maven打包时会编译特定文件导致文件不可用的问题 -->
58+
<nonFilteredFileExtensions>
59+
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
60+
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
61+
</nonFilteredFileExtensions>
62+
</configuration>
63+
</plugin>
5364
</plugins>
65+
<resources>
66+
<resource>
67+
<directory>src/main/resources</directory>
68+
<filtering>true</filtering>
69+
<includes>
70+
<!-- 将 resources 目录下的所有文件都一起打包 -->
71+
<include>**/*.*</include>
72+
</includes>
73+
</resource>
74+
</resources>
5475
</build>
5576

5677
</project>

src/main/java/com/ramostear/application/controller/FileController.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.ramostear.application.model.FileInfo;
44
import com.ramostear.application.util.FileUtil;
5+
import org.apache.tomcat.util.http.fileupload.IOUtils;
56
import org.springframework.beans.factory.annotation.Value;
67
import org.springframework.core.io.InputStreamResource;
8+
import org.springframework.core.io.Resource;
9+
import org.springframework.core.io.ResourceLoader;
710
import org.springframework.http.HttpHeaders;
811
import org.springframework.http.HttpStatus;
912
import org.springframework.http.MediaType;
@@ -14,10 +17,14 @@
1417
import org.springframework.web.multipart.MultipartFile;
1518

1619
import javax.annotation.PostConstruct;
20+
import javax.servlet.ServletOutputStream;
21+
import javax.servlet.http.HttpServletRequest;
22+
import javax.servlet.http.HttpServletResponse;
1723
import java.io.*;
1824
import java.util.Collection;
1925
import java.util.HashMap;
2026
import java.util.Map;
27+
import java.util.Objects;
2128

2229
/**
2330
* @author : ramostear
@@ -39,6 +46,9 @@ public class FileController {
3946

4047
private static Map<String,FileInfo> fileRepository = new HashMap<>();
4148

49+
// @Resource
50+
private ResourceLoader resourceLoader;
51+
4252
@PostConstruct
4353
public void initFileRepository(){
4454
FileInfo file1 = new FileInfo ().setFileName ( "bg1.jpg" );
@@ -90,24 +100,35 @@ public String fileUpload(@RequestParam("file") MultipartFile file) throws IOExce
90100

91101
@GetMapping("/download/{fileName}")
92102
@ResponseBody
93-
public ResponseEntity<Object> downloadFile(@PathVariable(name = "fileName") String fileName) throws FileNotFoundException {
94-
95-
File file = new File ( fileUploadRootDir+fileName);
96-
InputStreamResource resource = new InputStreamResource ( new FileInputStream ( file ) );
97-
98-
HttpHeaders headers = new HttpHeaders();
99-
headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s",fileName));
100-
headers.add ( "Cache-Control","no-cache,no-store,must-revalidate" );
101-
headers.add ( "Pragma","no-cache" );
102-
headers.add ( "Expires","0" );
103-
104-
ResponseEntity<Object> responseEntity = ResponseEntity.ok()
105-
.headers ( headers )
106-
.contentLength ( file.length ())
107-
.contentType(MediaType.parseMediaType ( "application/txt" ))
108-
.body(resource);
109-
110-
return responseEntity;
103+
public void downloadFile(@PathVariable(name = "fileName") String fileName,
104+
HttpServletResponse response){
105+
try {
106+
//获取要下载的模板名称
107+
//设置要下载的文件的名称
108+
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
109+
//通知客服文件的MIME类型
110+
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
111+
//获取文件的路径
112+
// String filePath = getClass().getResource("/"+fileName).getPath();
113+
InputStream input = getClass().getClassLoader().getResourceAsStream(fileName);
114+
// ByteArrayOutputStream out = new ByteArrayOutputStream();
115+
// System.out.println(filePath);
116+
// FileInputStream input = new FileInputStream(filePath);
117+
OutputStream out = response.getOutputStream();
118+
119+
byte[] b = new byte[2048];
120+
int len;
121+
while ((len = input.read(b)) != -1) {
122+
out.write(b, 0, len);
123+
}
124+
out.flush();
125+
out.close();
126+
input.close();
127+
} catch (Exception ex) {
128+
// log.error("getApplicationTemplate :", ex);
129+
System.out.println(ex);
130+
//return Response.ok("应用导入模板下载失败!");
131+
}
111132
}
112133

113134

src/main/resources/1234.xlsx

9.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)