文件下载介绍
分析前端代码
好吧,我看不懂
文件下载代码实现
编写CommonCotroller.java
/**
* 文件下载
* @param name
* @param response
*/
@GetMapping("/download")
public void download(String name, HttpServletResponse response){
try {
//输入流,通过输入流读取文件内容
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
//输出流,通过输出流将文件写回浏览器,在浏览器展示图片了
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("imag/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while((len = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
outputStream.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
System.out.println("输入流 error...");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Io异常...");
e.printStackTrace();
}
}
测试功能
回显成功