实现点击图片下载
实现点击图片下载
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>
还没有评论,来说两句吧...