/**
* 下载文件
**/
@RequestMapping(value="/downloadFileNoLogin")
public void downloadFile(@RequestParam String fileName, String filePath){
String newFileName = "";
if(fileName != null){
try {
fileName = new String(fileName.getBytes("ISO-8859-1"),"UTF-8");
filePath = new String(filePath.getBytes("ISO-8859-1"),"UTF-8");
//newFileName = new String(fileName.getBytes(), "ISO-8859-1");
newFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
File file = new File(filePath);
if(file.exists()){
response.setCharacterEncoding("UTF-8");
response.setContentType("application/force-download"); //设置强制下载不打开
response.addHeader("Content-Disposition","attachment;fileName=" + newFileName); // 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(bis != null){
bis.close();
}
if(fis != null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
return;
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}else{
return;
}
}
文件下载
最新推荐文章于 2020-06-17 09:15:09 发布