public static void DownLoadFile(String filePath, String fileName,
HttpServletResponse response) throws Exception {
System.out.println("filepath:" + filePath);
File file = new File(filePath);
if (!file.exists()) {
System.out.println("文件不存在");
} else {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
fileName = URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "GBK");
response.reset();
response.setContentType("UTF-8");
response.setContentType("Application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.setHeader("Content-Length", String
.valueOf(bis.available()));
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
bos.close();
bis.close();
os.close();
fis.close();
}
}
java 设置下载文件名 java下载文件夹代码
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
centos7用命令上传、下载文件
centos7用命令上传、下载文件
centos linux 上传下载