22
33import com .ramostear .application .model .FileInfo ;
44import com .ramostear .application .util .FileUtil ;
5+ import org .apache .tomcat .util .http .fileupload .IOUtils ;
56import org .springframework .beans .factory .annotation .Value ;
67import org .springframework .core .io .InputStreamResource ;
8+ import org .springframework .core .io .Resource ;
9+ import org .springframework .core .io .ResourceLoader ;
710import org .springframework .http .HttpHeaders ;
811import org .springframework .http .HttpStatus ;
912import org .springframework .http .MediaType ;
1417import org .springframework .web .multipart .MultipartFile ;
1518
1619import javax .annotation .PostConstruct ;
20+ import javax .servlet .ServletOutputStream ;
21+ import javax .servlet .http .HttpServletRequest ;
22+ import javax .servlet .http .HttpServletResponse ;
1723import java .io .*;
1824import java .util .Collection ;
1925import java .util .HashMap ;
2026import 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
0 commit comments