springboot实现下载图片

 时间:2026-02-14 23:20:32

1、第一步:搭建springboot环境。

file---》new--》project..--》设置项目名称download,添加web依赖

springboot实现下载图片

springboot实现下载图片

springboot实现下载图片

springboot实现下载图片

springboot实现下载图片

2、第二步:编辑代码。

具体代码如下所示

@Component@Scope("prototype")@RequestMapping("/downloadImg")public class DownloadController {   @RequestMapping("download")   public ResponseEntity<byte[]> 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<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);       return entity;   }}

springboot实现下载图片

3、第三步:测试功能。

1、打开谷歌或者其它浏览器。

2、输入地址如下图所示

3,、素材如下

springboot实现下载图片

springboot实现下载图片

springboot实现下载图片

  • jsp中的forward
  • PyQt5-QWidget类控件位置大小设置操作
  • js中的map 遍历方法
  • IntelliJ IDEA怎么关闭代码折叠关闭
  • myeclipse如何查找替换项目关键字
  • 热门搜索
    科普知识手抄报内容 安全手抄报的图片 尊师手抄报 感恩父母手抄报内容 节约用水的手抄报内容 关于友善的手抄报 英语手抄报内容5年级 致敬抗美援朝争做时代新人手抄报 清明手抄报资料 庆祝国庆节手抄报