Java下载文件
//设置文件路径
File file = new File(filePath);
//判断文件是否存在
if (file.exists()) {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream 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);
}
bis.close();
fis.close();
return AjaxResult.success("下载成功");
} else {
return AjaxResult.error("下载失败");
}