Java SpringBoot实现文件上传功能的示例代码

网友投稿 362 2022-08-21


Java SpringBoot实现文件上传功能的示例代码

测试代码

pom.xml:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.2

jar

com.kaven

springboot

0.0.1-SNAPSHOT

springboot

springboot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.6.2

jar

com.kaven

springboot

0.0.1-SNAPSHOT

springboot

springboot

1.8

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

org.springframework.boot

spring-boot-maven-plugin

application.properties(配置文件):

spring.servlet.multipart.max-file-size=500MBspring.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 – http://要读取的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进行测试。

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

上传文件不存在。

控制台的输出。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java中int转string与string转int的效率对比
下一篇:Java十分钟精通包装类的使用与操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~