Skip to content

修复下载中文文件bug #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -90,13 +91,14 @@ public String fileUpload(@RequestParam("file") MultipartFile file) throws IOExce

@GetMapping("/download/{fileName}")
@ResponseBody
public ResponseEntity<Object> downloadFile(@PathVariable(name = "fileName") String fileName) throws FileNotFoundException {
public ResponseEntity<Object> downloadFile(@PathVariable(name = "fileName") String fileName) throws FileNotFoundException, UnsupportedEncodingException {

File file = new File ( fileUploadRootDir+fileName);
InputStreamResource resource = new InputStreamResource ( new FileInputStream ( file ) );

HttpHeaders headers = new HttpHeaders();
headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s",fileName));
// 使用URLEncoder.encode(fileName, "UTF-8") 下载文件能正常显示中文
headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s", URLEncoder.encode(fileName, "UTF-8")));
headers.add ( "Cache-Control","no-cache,no-store,must-revalidate" );
headers.add ( "Pragma","no-cache" );
headers.add ( "Expires","0" );
Expand Down
9 changes: 0 additions & 9 deletions src/main/resources/application.properties

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
spring:
freemarker:
cache: false
charset: UTF-8
enabled: true
prefix: ''
suffix: .html
template-loader-path: classpath:/templates/
servlet:
multipart:
max-file-size: 100MB
max-request-size: 1000MB
file:
upload:
root:
dir:
linux: ~/upload/
mac: /users/gaohanghang/upload/
windows: C:/work/upload/