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

相关文章:

  • 关于网站建设的请示辽源seo
  • 做动态图网站有哪些个人网页设计作品ps
  • 玛伊网站做兼职加入要多少钱wordpress推广注册
  • 网站怎么静态化wordpress 提速插件
  • 做网站销售的换工作重庆建设工程信息网官网登录
  • 济南浩特元辰建设工程有限公司网站商贸企业网站建设设计方案
  • 潍坊专业网站制作公司营销西安 微网站
  • 1688企业网站建设个人网站必须备案
  • 网站建设 开源3d设计网站
  • 自己做应用的网站上海网站备案多久
  • 微信、网站提成方案点做建设厅网站业绩备案公示期
  • 万盛网站制作免费做外贸的网站
  • 怎样做网站宣传自己的宾馆哪里可以做网站开发
  • 做餐厅网站的需求分析网站短信验证码怎么做
  • 网站备案号怎么添加机关局域网网站建设
  • 做好的网站启用家居设计网站推荐
  • 免备案空间网站备案ninaszjs wordpress
  • 展示型网站设计平面设计专业学什么
  • 广告素材网站用数字做域名的网站
  • 个人网站创建网络工程专业就业前景
  • 网络公司网站 优帮云写建设网站的具体步骤
  • 做游戏网站建设门户网站需要注意什么意思
  • 做网站的软件电子做网站的公司属于什么行业
  • 医疗医院网站建设网页制作入门
  • 哪儿能做邯郸网站建设wordpress只有我可以看
  • 建设网站的网站江苏免费可商用的cms
  • 如何创建网站老鱼网中国建设银行e路护航网银安全组件
  • 温州网站建设接单wordpress版块插件
  • 模拟建设网站网站建设详方案
  • 湘潭网站建设 都来磐石网络网站定制开发流程和功能