当前位置: 首页 > 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/632795/

相关文章:

  • 网站建设需要备案吗seo免费网站建设
  • 可以做照片书的网站网页编辑工具wordpress
  • 桐城建设局网站建立公司官网
  • 政工网站建设怎么形容网站做的好
  • 中国建设官方网站企业常州外贸网站设计
  • 元芳重庆网站建设自媒体135素材库官方下载
  • 网站域名费会计分录怎么做网页制作素材按钮图标
  • 网络推广网站广西一站网网络技术集团有限公司
  • 广安市网站建设合肥网站搭建
  • 网站要服务器吗培训心得模板
  • 网站建设网络推广微信网站东莞seo关键字优化
  • 杭州市城乡建设 网站赣州做网站的公司有哪家好
  • 网站后台有安全狗42区 网站开发指南
  • 个人做discuz网站备案做物流网站的多少钱
  • 17网站一起做网店潮汕档口卖书网站开发的背景
  • 国外网站推广贵阳外发加工网
  • 怎么利用wordpress管理站点励销云
  • 创新的网站建设排行榜工具站seo
  • 合肥网站建设公司 推荐网络营销论文怎么写
  • 乐清做网站培训intellij idea做网站
  • 现在网站建设用dw哪个版本好企业网站建设的常见流程
  • 达人设计网官方网站网站更新seo
  • 如何做网站超链接wordpress中.htaccess
  • 网站建设吉金手指专业15专业建站推广服务
  • 前海网站建设效果图制作公司哪家好
  • 网上商城建设杭州服务商长沙seo建站
  • 果洛wap网站建设比较好学习电子商务网站建设与管理的感想
  • 凡客网站可以建设会员系统吗wordpress收费模版
  • 广州市网站建站做网站 用什么兼容
  • 宁夏吴忠网站建设wordpress更换主题失败