Java SpringBoot实现文件上传功能的示例代码
作者:m0_54850467 时间:2022-05-23 02:54:46
测试代码
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<packaging>jar</packaging>
<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties(配置文件):
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB
max-file-size:指定允许上传文件的最大大小,默认值为1MB。
max-request-size:指定允许multipart/form-data请求的最大大小,默认值为10MB。
上传接口:
package com.kaven.springboot.controller;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.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;
@RestController
public 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;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringbootApplication.class);
application.run(args);
}
}
使用Postman
进行测试。
上传的文件是完整的,可以播放(视频文件)。
上传文件不存在。
控制台的输出。
来源:https://blog.csdn.net/m0_54850467/article/details/123427555
标签:Java,SpringBoot,文件,上传
0
投稿
猜你喜欢
SpringBoot 实现自定义的 @ConditionalOnXXX 注解示例详解
2023-04-04 03:37:18
JAVA使用JDBC连接oracle数据库的详细过程
2021-11-20 19:20:19
SpringBoot 如何实时刷新静态文件
2023-11-02 13:27:09
Java应用开源框架实现简易web搜索引擎
2023-08-22 20:20:54
Java语法中Lambda表达式无法抛出异常的解决
2022-10-13 01:04:43
javax.persistence中@Column定义字段类型方式
2021-12-03 21:21:44
springboot常用注释的讲解
2023-11-03 02:53:15
Java中闭包简单代码示例
2023-11-08 23:09:48
JAVA 实现磁盘文件加解密操作的示例代码
2023-11-15 00:13:06
Java C++实现相同MD5加密算法的方式
2023-10-12 12:26:02
MyBatis逆向⼯程的生成过程
2023-11-28 17:29:22
Java:泛型知识知多少
2023-11-24 23:08:44
java.lang.NoClassDefFoundError错误解决办法
2021-12-29 03:52:27
基于Class.forName()用法及说明
2021-06-20 19:09:30
Mybatis Log Plugin的使用方式
2021-08-27 19:02:31
Android利用Flutter实现立体旋转效果
2023-06-20 08:20:32
java模拟实现双向链表
2022-06-05 09:16:16
Android TextView实现跑马灯效果的方法
2023-07-30 20:44:12
SpringBoot上传文件大小受限问题的解决办法
2023-04-19 09:46:16
java 并发线程个数的如何确定
2022-01-01 21:52:13