实现点击图片下载

来源:互联网 发布:淘宝神器返利是真的吗 编辑:程序博客网 时间:2024/06/05 02:27
实现点击图片下载

1.编写java代码

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

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

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



原创粉丝点击