如何使用Java SpringBoot实现文件上传功能
发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,小编给大家分享一下如何使用Java SpringBoot实现文件上传功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!测试代码pom.xml:
千家信息网最后更新 2024年09月30日如何使用Java SpringBoot实现文件上传功能
小编给大家分享一下如何使用Java SpringBoot实现文件上传功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
测试代码
pom.xml:
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=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;@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实现文件上传功能"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
文件
字节
输入
输出
功能
最大
成功
大小
篇文章
错误
测试
原始
代码
参数
完了
实例
接口
控制台
文件名
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
绩优智酷管理软件开发商
网络技术综合实战平台
江西网络安全审计工控整机
龙华网络数据库维护怎么样
网络安全 精彩一课
数据库mdf如何附加
苹果手机 软件开发IDE
私自运营服务器什么罪
信息与网络安全教材
无线网络安全论坛
svn中如何查看数据库
数据库安全系统方案
wifi网络安全类型不正确
网络安全的七个知识
自然资源系统网络安全总结
郑州哪里有学习软件开发
学校财务部的网络安全设计图
绿盟科技工业互联网龙头
免费自动生成数据库
网络安全威胁检测方法论
服务器部署与管理项目背景
跨文化大数据库
观看守护晴朗网络安全感想
教务系统数据库安全技术
企业网站服务器租用
拨打座机号码无法连接服务器
云南创新软件开发报价表
口碑网站服务器托管
云南pdu服务器电源采购
小狗电器互联网科技 北京)