多语言展示
当前在线:1083今日阅读:26今日分享:39

springboot实现下载图片

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
工具/原料
1

电脑

2

intellij IDEA

方法/步骤
1

第一步:搭建springboot环境。file---》new--》project..--》设置项目名称download,添加web依赖

2

第二步:编辑代码。具体代码如下所示@Component@Scope('prototype')@RequestMapping('/downloadImg')public class DownloadController {   @RequestMapping('download')   public ResponseEntity download(HttpServletRequest request) throws IOException {       //读取文件       File file = new File('D:/img/火影2.jpg');       byte[] body = null;       InputStream is = new FileInputStream(file);       body = new byte[is.available()];       is.read(body);       HttpHeaders headers = new HttpHeaders();       //设置文件头       headers.add('Content-Disposition', 'attchement;filename=' + new String( '火影2.jpg'.getBytes('gb2312'), 'ISO8859-1' ));       HttpStatus statusCode = HttpStatus.OK;       ResponseEntity entity = new ResponseEntity(body, headers, statusCode);       return entity;   }}

3

第三步:测试功能。1、打开谷歌或者其它浏览器。2、输入地址如下图所示3,、素材如下

注意事项
1

intellij IDEA 2018 2.2

2

jdk 1.8以上

推荐信息