修改了一些代码的注释,修改了文件下载会因为浏览器不同导致下载失败的问题
This commit is contained in:
parent
479957fbc1
commit
bd5a16ed19
|
|
@ -1,71 +1,74 @@
|
||||||
package com.ruoyi.web.controller.common;
|
package com.ruoyi.web.controller.common;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import com.ruoyi.common.config.Global;
|
||||||
import java.net.URLEncoder;
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import com.ruoyi.common.config.Global;
|
|
||||||
import com.ruoyi.common.utils.file.FileUtils;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用请求处理
|
* 通用请求处理
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
public class CommonController
|
public class CommonController {
|
||||||
{
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件下载
|
||||||
|
*
|
||||||
|
* @param fileName
|
||||||
|
* @param delete
|
||||||
|
* @param response
|
||||||
|
* @param request
|
||||||
|
*/
|
||||||
@RequestMapping("common/download")
|
@RequestMapping("common/download")
|
||||||
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request)
|
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
|
||||||
{
|
|
||||||
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
String filePath = Global.getDownloadPath() + fileName;
|
String filePath = Global.getDownloadPath() + fileName;
|
||||||
|
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.setContentType("multipart/form-data");
|
response.setContentType("multipart/form-data");
|
||||||
response.setHeader("Content-Disposition", "attachment;fileName=" + setFileDownloadHeader(request, realFileName));
|
response.setHeader("Content-Disposition", "attachment;fileName=" + setFileDownloadHeader(request, realFileName));
|
||||||
FileUtils.writeBytes(filePath, response.getOutputStream());
|
FileUtils.writeBytes(filePath, response.getOutputStream());
|
||||||
if (delete)
|
if (delete) {
|
||||||
{
|
|
||||||
FileUtils.deleteFile(filePath);
|
FileUtils.deleteFile(filePath);
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
log.error("下载文件失败", e);
|
log.error("下载文件失败", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
|
/**
|
||||||
{
|
* 判断浏览器
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param fileName
|
||||||
|
* @return
|
||||||
|
* @throws UnsupportedEncodingException
|
||||||
|
*/
|
||||||
|
public String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
|
||||||
final String agent = request.getHeader("USER-AGENT");
|
final String agent = request.getHeader("USER-AGENT");
|
||||||
String filename = fileName;
|
String filename = fileName;
|
||||||
if (agent.contains("MSIE"))
|
if (agent.contains("MSIE")) {
|
||||||
{
|
|
||||||
// IE浏览器
|
// IE浏览器
|
||||||
filename = URLEncoder.encode(filename, "utf-8");
|
filename = URLEncoder.encode(filename, "utf-8");
|
||||||
filename = filename.replace("+", " ");
|
filename = filename.replace("+", " ");
|
||||||
}
|
} else if (agent.contains("Firefox")) {
|
||||||
else if (agent.contains("Firefox"))
|
|
||||||
{
|
|
||||||
// 火狐浏览器
|
// 火狐浏览器
|
||||||
filename = new String(fileName.getBytes(), "ISO8859-1");
|
filename = new String(fileName.getBytes(), "ISO8859-1");
|
||||||
}
|
} else if (agent.contains("Chrome")) {
|
||||||
else if (agent.contains("Chrome"))
|
|
||||||
{
|
|
||||||
// google浏览器
|
// google浏览器
|
||||||
filename = URLEncoder.encode(filename, "utf-8");
|
filename = URLEncoder.encode(filename, "utf-8");
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
// 其它浏览器
|
// 其它浏览器
|
||||||
filename = URLEncoder.encode(filename, "utf-8");
|
filename = URLEncoder.encode(filename, "utf-8");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue