实现点击图片下载

系统管理员 2022-06-05 00:10 405阅读 0赞

实现点击图片下载

1.编写java代码

  1. /**
  2. * 点击图片下载
  3. * @param imagePath
  4. * @return
  5. * @throws Exception
  6. */
  7. @RequestMapping(value="/download",method=RequestMethod.GET)
  8. public ResponseEntity<byte[]> loadFoodimg(String imagePath) throws Exception{
  9. String absPath="E:\\apache-tomcat-6.0.45\\webapps\\springmvcLesson\\image\\"+imagePath;
  10. String fileName=imagePath;
  11. //需要下载的目标文件
  12. File file = new File(absPath);
  13. //设置响应头
  14. HttpHeaders hh = new HttpHeaders();
  15. //设置下载的文件的名称
  16. hh.setContentDispositionFormData("attachment", URLEncoder.encode(fileName,"UTF-8"));
  17. //读取目标文件为二进制数组
  18. byte [] fileByte = FileCopyUtils.copyToByteArray(file);
  19. //创建ResponseEntity对象
  20. ResponseEntity<byte[]> result = new ResponseEntity<byte[]>(fileByte,hh,HttpStatus.CREATED);
  21. return result;
  22. }

2.xml 实现 图片为一个链接 点击下载

  1. <td><a href='../download?imagePath=${foodList.imgpath}'><image src="../image/${foodList.imgpath}"></a></td>

发表评论

表情:
评论列表 (有 0 条评论,405人围观)

还没有评论,来说两句吧...

相关阅读