!196 修复不同浏览器附件下载中文名乱码的问题

Merge pull request !196 from zhengxiaolong/master
pull/196/MERGE
若依 2020-08-16 10:56:13 +08:00 committed by Gitee
commit 4b8d578f4d
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());
}
}

View File

@ -7,7 +7,9 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
@ -139,4 +141,37 @@ public class FileUtils extends org.apache.commons.io.FileUtils
}
return filename;
}
/**
*
*
* @param response
* @param realFileName
* @return
*/
public static void setAttachmentResponseHeader(HttpServletResponse response,String realFileName) throws UnsupportedEncodingException {
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
response.setHeader("Content-disposition",
contentDispositionValue.toString());
}
/**
*
*
* @param s
* @return
*/
public static String percentEncode(String s) throws UnsupportedEncodingException {
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode .replaceAll("\\+", "%20");
}
}