java文件
@RequestMapping("/download")
public ResponseEntity<byte[]> dowload() throws IOException{
String path="D:/沈雪冰.png";
File file=new File(path);
String fileName=new String(file.getName().getBytes("utf-8"),"iso-8859-1"); //解决中文乱码问题
HttpHeaders headers=new HttpHeaders();
headers.setContentDispositionFormData("attachment", fileName);// aatachment 附件
headers.setContentType(MediaType.IMAGE_PNG);
ResponseEntity<byte[]> entity=new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers,HttpStatus.CREATED);
return entity;
}
springmvc.xml配置
<!-- 文件上传下载配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="500000"></property>
<property name="maxInMemorySize" value="50000"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>
运行结果