|
@@ -30,6 +30,7 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
@@ -208,37 +209,34 @@ public class FileServiceImpl extends ServiceImpl<FileStorageDAO,FileStorageDO> i
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ResponseEntity<Object> downloadFile(Long id) {
|
|
|
+ public ResponseEntity<Object> downloadFile(Long id,HttpServletResponse res) {
|
|
|
FileStorageDO fileStorage = this.getById(id);
|
|
|
if (fileStorage != null) {
|
|
|
String fileName = fileStorage.getOriginalName();
|
|
|
- String fileNameEncodeStr = "null";
|
|
|
- try {
|
|
|
- fileNameEncodeStr = java.net.URLEncoder.encode(fileName, "UTF-8");
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- fileNameEncodeStr = "";
|
|
|
- }
|
|
|
String path = fileStorage.getAbsolutePath();
|
|
|
File file = new File(path);
|
|
|
-
|
|
|
try {
|
|
|
- InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
|
|
-
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
-// headers.add("Content-Disposition", "attachment; filename=" + fileNameEncodeStr);
|
|
|
-// headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
|
|
|
- headers.add("Content-Disposition", "attachment;filename=" + fileNameEncodeStr);
|
|
|
- headers.add("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
- headers.add("Pragma", "no-cache");
|
|
|
- headers.add("Expires", "0");
|
|
|
-
|
|
|
- ResponseEntity<Object> responseEntity = ResponseEntity.ok()
|
|
|
- .headers(headers)
|
|
|
- .contentLength(file.length())
|
|
|
- .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
|
|
- .body(resource);
|
|
|
- return responseEntity;
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
+ if (file.exists()) {
|
|
|
+ fileName = new String(fileName.getBytes("UTF-8"));
|
|
|
+ String basePath = file.getAbsolutePath();
|
|
|
+ InputStream in = new FileInputStream(new File(basePath));
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
+// res.setHeader("content-disposition", "attachment;filename=" + fileName);
|
|
|
+// res.setHeader("Content-Type", "application/vnd.ms-exce");
|
|
|
+ res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
|
|
+ res.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
|
|
+ OutputStream out = res.getOutputStream();
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int len = -1;
|
|
|
+ while ((len = in.read(b)) != -1) {
|
|
|
+ out.write(b, 0, len);
|
|
|
+ out.flush();
|
|
|
+ }
|
|
|
+ // 关闭
|
|
|
+ in.close();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|