//在springmvc中配置
class="org.springframework.http.converter.StringHttpMessageConverter"/>
class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
class="org.springframework.http.converter.StringHttpMessageConverter" />
//controller写法
@RequestMapping("download.do")
public ResponseEntity<byte[]> download(HttpServletRequest request,HttpServletResponse response,String pictureFile) throws Exception{
File file=new File("F:\\upload\\image\\"+pictureFile);
HttpHeaders headers = new HttpHeaders();
pictureFile=new String(pictureFile.getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", pictureFile);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}