当前位置: 首页 > news >正文

做第三方库网站北京网站建设的价格天

做第三方库网站,北京网站建设的价格天,做网站需要什么域名,优秀网页前言:这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能,一共三种常见的下载方式和一种上传方式,特此做一个笔记分享。 目录 一、pom依赖 二、yml配置文件 三、文件下载 3.1、使用Spring框架提供的下载方式 3.2、通过IOUti…

前言:这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能,一共三种常见的下载方式和一种上传方式,特此做一个笔记分享。

目录

一、pom依赖

二、yml配置文件

三、文件下载

3.1、使用Spring框架提供的下载方式

3.2、通过IOUtils以流的形式下载

3.3、边读边下载

四、文件上传

五、工具类完整代码

六、Gitee源码 

七、总结


一、pom依赖

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

二、yml配置文件

# Spring配置
spring:# 文件上传servlet:multipart:# 单个文件大小max-file-size: 10MB# 设置总上传的文件大小max-request-size: 20MB
server:port: 9090

三、文件下载

3.1、使用Spring框架提供的下载方式

关键代码:

    /*** 使用Spring框架自带的下载方式* @param filePath* @param fileName* @return*/public ResponseEntity<Resource> download(String filePath,String fileName) throws Exception {fileName = URLEncoder.encode(fileName,"UTF-8");File file = new File(filePath);if(!file.exists()){throw new Exception("文件不存在");}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=" + fileName ).body(new FileSystemResource(filePath));}

请求层:

@RestController
@RequestMapping("/file")
public class FileController {@Autowiredprivate FileUtil fileUtil;@GetMapping("/spring/download")public ResponseEntity<Resource> download() throws Exception {String filePath = "D:\\1.jpg";String fileName = "Spring框架下载.jpg";return fileUtil.download(filePath,fileName);}}

浏览器输入:http://localhost:9090/file/spring/download 

 

下载完成。 

3.2、通过IOUtils以流的形式下载

关键代码:

    /*** 通过IOUtils以流的形式下载* @param filePath* @param fileName* @param response*/public void download(String filePath , String fileName, HttpServletResponse response) throws Exception {fileName = URLEncoder.encode(fileName,"UTF-8");File file=new File(filePath);if(!file.exists()){throw new Exception("文件不存在");}response.setHeader("Content-disposition","attachment;filename="+ fileName);FileInputStream fileInputStream = new FileInputStream(file);IOUtils.copy(fileInputStream,response.getOutputStream());response.flushBuffer();fileInputStream.close();}

请求层: 

@RestController
@RequestMapping("/file")
public class FileController {@Autowiredprivate FileUtil fileUtil;@GetMapping("/io/download")public void ioDownload(HttpServletResponse response) throws Exception {String filePath = "D:\\1.jpg";String fileName = "IO下载.jpg";fileUtil.download(filePath,fileName,response);}}

浏览器访问:http://localhost:9090/file/io/download

下载成功。 

3.3、边读边下载

关键代码:

    /*** 原始的方法,下载一些小文件,边读边下载的* @param filePath* @param fileName* @param response* @throws Exception*/public void downloadTinyFile(String filePath,String fileName, HttpServletResponse response)throws Exception{File file = new File(filePath);fileName = URLEncoder.encode(fileName, "UTF-8");if(!file.exists()){throw new Exception("文件不存在");}FileInputStream in = new FileInputStream(file);response.setHeader("Content-Disposition", "attachment;filename="+fileName);OutputStream out = response.getOutputStream();byte[] b = new byte[1024];int len = 0;while((len = in.read(b))!=-1){out.write(b, 0, len);}out.flush();out.close();in.close();}

请求层:

@RestController
@RequestMapping("/file")
public class FileController {@Autowiredprivate FileUtil fileUtil;@GetMapping("/tiny/download")public void tinyDownload(HttpServletResponse response) throws Exception {String filePath = "D:\\1.jpg";String fileName = "tiny下载.jpg";fileUtil.downloadTinyFile(filePath,fileName,response);}}

浏览器输入:http://localhost:9090/file/tiny/download 

 

下载成功。

四、文件上传

使用MultipartFile上传文件

    /*** 上传文件* @param multipartFile* @param storagePath* @return* @throws Exception*/public String upload(MultipartFile multipartFile, String storagePath) throws Exception{if (multipartFile.isEmpty()) {throw new Exception("文件不能为空!");}String originalFilename = multipartFile.getOriginalFilename();String newFileName = UUID.randomUUID()+"_"+originalFilename;String filePath = storagePath+newFileName;File file = new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}multipartFile.transferTo(file);return filePath;}

请求层:

@RestController
@RequestMapping("/file")
public class FileController {@Autowiredprivate FileUtil fileUtil;@PostMapping("/multipart/upload")public String download(MultipartFile file) throws Exception {String storagePath = "D:\\";return fileUtil.upload(file,storagePath);}}

使用postman工具测试:

在D盘找到此文件。 

五、工具类完整代码

package com.example.file.utils;import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;/*** 文件工具类* @author HTT*/
@Component
public class FileUtil {/*** 使用Spring框架自带的下载方式* @param filePath* @param fileName* @return*/public ResponseEntity<Resource> download(String filePath,String fileName) throws Exception {fileName = URLEncoder.encode(fileName,"UTF-8");File file = new File(filePath);if(!file.exists()){throw new Exception("文件不存在");}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=" + fileName ).body(new FileSystemResource(filePath));}/*** 通过IOUtils以流的形式下载* @param filePath* @param fileName* @param response*/public void download(String filePath , String fileName, HttpServletResponse response) throws Exception {fileName = URLEncoder.encode(fileName,"UTF-8");File file=new File(filePath);if(!file.exists()){throw new Exception("文件不存在");}response.setHeader("Content-disposition","attachment;filename="+ fileName);FileInputStream fileInputStream = new FileInputStream(file);IOUtils.copy(fileInputStream,response.getOutputStream());response.flushBuffer();fileInputStream.close();}/*** 原始的方法,下载一些小文件,边读边下载的* @param filePath* @param fileName* @param response* @throws Exception*/public void downloadTinyFile(String filePath,String fileName, HttpServletResponse response)throws Exception{File file = new File(filePath);fileName = URLEncoder.encode(fileName, "UTF-8");if(!file.exists()){throw new Exception("文件不存在");}FileInputStream in = new FileInputStream(file);response.setHeader("Content-Disposition", "attachment;filename="+fileName);OutputStream out = response.getOutputStream();byte[] b = new byte[1024];int len = 0;while((len = in.read(b))!=-1){out.write(b, 0, len);}out.flush();out.close();in.close();}/*** 上传文件* @param multipartFile* @param storagePath* @return* @throws Exception*/public String upload(MultipartFile multipartFile, String storagePath) throws Exception{if (multipartFile.isEmpty()) {throw new Exception("文件不能为空!");}String originalFilename = multipartFile.getOriginalFilename();String newFileName = UUID.randomUUID()+"_"+originalFilename;String filePath = storagePath+newFileName;File file = new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}multipartFile.transferTo(file);return filePath;}}

六、Gitee源码 

码云地址:SpringBoot实现文件上传和下载

七、总结

以上就是SpringBoot实现文件上传和下载功能的笔记,一键复制使用即可。

http://www.yayakq.cn/news/420289/

相关文章:

  • 做国际贸易需要网站吗服务器搭建vps教程
  • 海南棋牌网站建设无代码做网站
  • 旅游公司网站开发网站会员注册模板
  • 高端网站建设苏州网站群管理平台
  • 网站由哪几个部分组成营销技巧培训
  • 前程无忧网站开发待遇怎么样做理财的网站有哪些
  • 好网站建设公司报价模仿wordpress主题
  • 怎么做淘宝客网站优化电子商务专业怎么样
  • 怎么查网站服务器恩施市建设银行网站
  • 建设通相似的网站分销商平台
  • 惠州建设局网站首页网站外链建设常用字
  • 深圳市seo网站设计多少钱泰安网络营销网站建设
  • 广州婚恋网站排名福州建网站
  • 外销网站怎么做的自己可以做网站吗
  • 长治在百度做个网站多少钱直播软件推荐
  • 通栏 网站模板六安网络上
  • 电商网站规划论文九江茶叶网站建设
  • 网站建设是什么专业啊怎么做服装外贸网站
  • dede小游戏php网站源码iis做网站文件下载
  • 长沙做手机网站建设免费网站报价单怎么做
  • 有个网站做彩盒的网页设计网站的设计与规划
  • 免费的毕业设计网站建设京东网站建设流程和结构图
  • 长春老火车站图片青岛网站推广怎么选
  • 交通网站建设如何选择家居网站建设
  • 在北京网站建设的岗位职责网站移动终端建设
  • 做民宿的有哪些网站大连制作网站报价
  • 无版权图片做网站wordpress文章cms模板
  • 多个链接的网站怎么做的wordpress与微信对接
  • 著名网站设计江都区城乡建设局网站马局
  • 上海外贸网站推广网站开发是做什么?