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

美术学院网站建设电商网站设计系统

美术学院网站建设,电商网站设计系统,网站公司技术交接,成都网站seo设计前言#xff1a;这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能#xff0c;一共三种常见的下载方式和一种上传方式#xff0c;特此做一个笔记分享。 目录 一、pom依赖 二、yml配置文件 三、文件下载 3.1、使用Spring框架提供的下载方式 3.2、通过IOUti… 前言这边汇总了一下目前SpringBoot项目当中常见文件上传和下载的功能一共三种常见的下载方式和一种上传方式特此做一个笔记分享。 目录 一、pom依赖 二、yml配置文件 三、文件下载 3.1、使用Spring框架提供的下载方式 3.2、通过IOUtils以流的形式下载 3.3、边读边下载 四、文件上传 五、工具类完整代码 六、Gitee源码  七、总结 一、pom依赖 dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/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 ResponseEntityResource 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 ResponseEntityResource 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 filenew 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;filenamefileName);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 storagePathnewFileName;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 ResponseEntityResource 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 filenew 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;filenamefileName);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 storagePathnewFileName;File file new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}multipartFile.transferTo(file);return filePath;}}六、Gitee源码  码云地址SpringBoot实现文件上传和下载 七、总结 以上就是SpringBoot实现文件上传和下载功能的笔记一键复制使用即可。
http://wiki.neutronadmin.com/news/211090/

相关文章:

  • 甘肃崇信县门户网站兰州专业网站建设公司哪家好
  • 柳州网站建设22电商网站在线支付怎么做
  • 建设银行信用卡管理中心网站首页网站开发不兼容ie8
  • 免费推广网站下载伊利集团的网站建设水平评价
  • 江苏省建设局网站首页调用百度地图做全景的网站
  • 怎样自己做代刷网站微信小程序介绍
  • pw域名网站温州品牌网站建设
  • 网站推广关键词排名怎么免费做个人网站
  • 深圳网站设计公司设计wordpress登录无效用户名
  • 个人怎样免费建网站幻影图片一键制作网站
  • 做标志的网站考幼师证去哪个网站做试题
  • win7 iis添加网站济南做网站最好的公司
  • 昌平网站建设推广优化seowordpress主题 单页
  • 外贸网站制作要求揭阳网站免费建站
  • 本地linux做网站六安网站制作多少钱
  • 网站做seo多少钱微信小程序跳转到网站
  • logo网站设计素材微妙音门户网站建设
  • 深圳网站 建设信科网络大学生网页设计期末作品代码
  • 卡片式设计的网站网站后台上传图片无法显示
  • 站长工具seo综合查询 分析现在阳性最新情况
  • wordpress 小程序主题seo 怎么做到百度首页
  • 上海网站群建设丽水集团网站建设
  • 网站建设与维护经营范围优设
  • php网站二次开发用什么软件手机网站图片自适应代码
  • 亚马逊欧洲站入口网址页面设计素材网站
  • 沧州北京网站建设阿里巴巴企业网站注册
  • 南京企业网站设计公司500元济源网站开发
  • 百度做网站好吗光谷网站建设哪家好
  • 宁夏住房和城乡建设官方网站百川网站维护
  • 内网网站建设的步骤过程做淘宝客网站好搭建吗?