提供图片下载接口
1.瀏覽器請求地址:
localhost:8080/download/img?path=home/ccsp/img/abc.jpg
2.后臺控制層
/**
?* 提供下載圖片的接口
?* @author: ljg
?* @Datetime: 2019年8月7日下午6:13:53
?* @ClassName:DownloadfilesController
?*/
@Controller
@RequestMapping("download")
public class DownloadfilesController {
?? ?
?? ?@RequestMapping("/img")
?? ?@ResponseBody
?? ?public void init(HttpServletResponse response, @RequestParam("path")String path) {
?? ??? ?try {
?? ??? ??? ?// 文件名
?? ??? ??? ?String fileName=path.substring(path.lastIndexOf("/")+1);
?? ??? ??? ?// 后綴名
?? ??? ??? ?String fileSuffixName=?? fileName.substring(fileName.lastIndexOf(".")+1);
?? ??? ??? ?// 清除緩存
?? ??? ??? ?response.reset();
?? ??? ??? ?FileInputStream fileInputStream = new FileInputStream(path);
?? ??? ??? ?ServletOutputStream outputStream = response.getOutputStream();
?? ??? ??? ?response.setContentType("application/" +fileSuffixName + ";" +"charset = UTF-8"); //設置字符集和文件后
?? ??? ??? ?response.setHeader("Content-Disposition", "attachment;filename=" +fileName+"."+fileSuffixName);
?? ??? ??? ?byte[] bytes = new byte[1024];
?? ??? ??? ?while ((fileInputStream.read(bytes)) != -1) {
?? ??? ??? ??? ?outputStream.write(bytes);
?? ??? ??? ?}
?? ??? ??? ?fileInputStream.close();
?? ??? ??? ?outputStream.flush();
?? ??? ??? ?outputStream.close();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
}
3.取消登錄URL攔截。
@WebFilter(filterName = "UrlFilter", urlPatterns = "/*", initParams = {@WebInitParam(name = "noFilterPath", value = "/download/img;...) })
總結
- 上一篇: qRT-PCR 注意事项
- 下一篇: 鱼缸里一条贪吃的鱼跳缸了