springboot文件上传和下载的实现,Springboot上传文件

  springboot文件上传和下载的实现,Springboot上传文件

  测试代码

  pom.xml:

  ?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本父groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-parent/artifactId版本2.6配置文件):

  春天。servlet。多部分。最大文件大小=500 MB

  最大文件大小:指定允许上传文件的最大大小,默认值为1MB。

  最大请求大小:指定允许多部分/表单数据请求的最大大小,默认值为10MB。

  上传接口:

  包com。凯文。弹簧靴。控制器;导入org。阿帕奇。雄猫。util。http。文件上传。iou tils导入org。spring框架。http。http状态;导入org。spring框架。util。字符串实用程序;导入org。spring框架。网络。绑定。注释。后期映射;导入org。spring框架。网络。绑定。伊斯兰教纪元

  tation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;@RestControllerpublic class FilesController { @PostMapping(value="/upload", headers="content-type=multipart/form-data") public String upload(@RequestParam(value = "file") MultipartFile file, HttpServletResponse response) throws IOException, InterruptedException { System.out.println("有文件上传请求进来了"); FileOutputStream fileOutputStream = null; InputStream inputStream = null; try { // 上传文件是否存在 if (file != null && !file.isEmpty()) { // 如果上传文件存在,获取它的原始文件名 String fileName = file.getOriginalFilename(); if (StringUtils.hasText(fileName)) { // 将上传文件存储在服务器的E盘下(Windows) java.io.File outFile = new java.io.File("E:" + fileName); // 基于outFile创建文件输出流实例 fileOutputStream = new FileOutputStream(outFile); // 获取上传文件的输入流 inputStream = file.getInputStream(); /* * 将字节从输入流复制到输出流 * 此方法在内部会缓冲输入,因此无需使用BufferedInputStream * 大型流(超过2GB)将在复制完成后返回字节复制值-1 ,因为无法将正确的字节数作为int返回 * 对于大型流,需要使用copyLarge(InputStream, OutputStream)方法 * 参数: * input – 要读取的InputStream * output - 要写入的OutputStream * */ IOUtils.copy(inputStream, fileOutputStream); } } else { // 文件不存在 response.setStatus(HttpStatus.BAD_REQUEST.value()); return "文件不存在"; } } catch (Exception e) { // 文件上传错误 e.printStackTrace(); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return "文件上传错误"; } finally { if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } // 文件上传成功 response.setStatus(HttpStatus.OK.value()); return "文件上传成功"; }}启动类:

  

package com.kaven.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(SpringbootApplication.class); application.run(args); }}
使用Postman进行测试。

  

  

  上传的文件是完整的,可以播放(视频文件)。

  

  上传文件不存在。

  

  控制台的输出。

  

  到此这篇关于Java SpringBoot实现文件上传功能的示例代码的文章就介绍到这了,更多相关SpringBoot文件上传内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: