public static Boolean download(HttpServletResponse response,String localPath,String fileName){
boolean flag = false;
try {
// path是指欲下载的文件的路径。
File file = new File(localPath);
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(localPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header,此处要对中文进行编码处理。
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
flag = true;
} catch (IOException ex) {
ex.printStackTrace();
}
return flag;
}
java下载本地文件
最新推荐文章于 2023-08-18 19:28:52 发布