2019-06-26:
由于是具体业务 ,没办法上图
刚刚改完,导出组里面的东西,组名+时间戳就是文件名
组名是另一个组的同事建的,所以有很多像(英文括号,英文逗号,下划线,中划线)之类的字符
遇到一个文件名全都有的,一个一个删,最后才发现只有英文逗号才会导致下载失败
没办法让另一个组的同事将逗号改成别的 ,我就在代码中用别的可以下载的符号代替了
记下来,下次遇到好查
2019-06-27:
今天发现,只要在下载中将文件名替换一下就OK了
下面这段代码是解决问题的灵魂
response.setHeader("Content-Disposition", "attachment; filename=" + fileName.replace(",", "."));
下面是完整的下载:
/**
* 文件下载
*
* @param fileName
* @param response
*/
public static boolean downLoadFile(HttpServletRequest request, HttpServletResponse response, String fileName) {
InputStream fis = null;
ServletOutputStream streamOut = null;
try {
request.setCharacterEncoding("UTF-8");
File file = new File(fileName.trim());
if (!file.exists()) {
return false;
}
fis = new BufferedInputStream(new FileInputStream(file));
streamOut = response.getOutputStream();
fileName = new String(file.getName().getBytes("utf-8"), "ISO8859-1");
response.reset();
response.setContentType("application/x-download;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName.replace(",", "."));
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = fis.read(buffer, 0, 1024)) > 0) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
IOUtils.closeQuietly(streamOut);
IOUtils.closeQuietly(fis);
}
return true;
}