修复不同浏览器下附件中文名乱码的bug

采用[RFC 6266]定义的标注写法,兼容市面上所有主流浏览器
详情请浏览本人原创文章:https://segmentfault.com/a/1190000023601065
This commit is contained in:
zhengxl5566
2020-08-14 15:05:13 +08:00
parent d957acd899
commit b369317800
2 changed files with 47 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -18,6 +19,10 @@ import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* 通用请求处理
*
@ -49,10 +54,9 @@ public class CommonController
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = Global.getDownloadPath() + fileName;
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response,realFileName);
FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete)
{
@ -103,10 +107,10 @@ public class CommonController
String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
// 下载名称
String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response,downloadName);
FileUtils.writeBytes(downloadPath, response.getOutputStream());
}
}