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

青岛网站设计软件无锡网站怎么做

青岛网站设计软件,无锡网站怎么做,深圳网站优化团队,有哪些可以做外链的网站前言#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://www.pierceye.com/news/477466/

相关文章:

  • 学院网站建设总结华北理工大学学科建设处网站
  • 简单的网站php开发教程用cms做的网站 的步骤
  • seo杭州seo快速排名利器
  • 谷歌不收录网站一个完整的短视频策划方案
  • 网页制作培训网站关于营销的最新的新闻
  • 免费查找资料的网站wordpress中文4.8
  • 凡科建设的网站如何中式建筑公司网站
  • 珠海网站建设品牌策划开发设计公司网站
  • 找别人做的网站怎样修改招聘app
  • 学校网站内容建设银行网站电脑上不去
  • 住建部工程建设标准网站上海室内设计事务所
  • 做外贸采购都是用什么网站网站重构方案
  • 企业网站做推广河南app开发
  • 海宁做网站的公司仿搜狐视频网站源码
  • 网站备案和不备案的上海制作网站公司网站
  • 网站建设专业介绍在线平面图设计
  • 临时工找工作网站做美缝手机网站不收录
  • 凡科建站怎么样网络推广网站培训班
  • 优惠券的网站怎么做的网站建设业务元提成
  • 网站开发项目组成员免费建网站的app
  • 怎样自己做公司网站驻马店logo设计公司
  • 知名网站制作公司排名徐州人才网最新招聘2023
  • 网站建设与网页设计难学吗做彩票的网站
  • 请问怎么做网站郑州小程序开发制作
  • 城乡建设网站职业查询系统小公司根本办不了icp许可证
  • 网站架构搭建搭建网站是什么专业
  • 互助网站建设电脑做网站端口映射
  • 电力行业做的好的招投标网站wordpress 自定义注册表单
  • 网站开发采集工具网站设计计划书的要求
  • 技术支持:佛山网站建设珠海网站制作服务