springboot多文件上传问题

在SpringBoot应用中遇到多文件上传问题,第一个文件上传成功但后续文件上传失败,原因是找不到临时文件.temp。通过更换文件上传工具,采用Apache的文件上传组件,并添加相关依赖,成功解决了问题。此外,还提及了一个未验证的配置解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题1.第一个文件上传成功,后面的文件上传失败(找不到临时文件.temp)

解决:1.换文件上传工具(原本用的是MultipartFile的transferTo()方法),使用了apache的上传文件工具。

添加依赖:`
	<dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
		<version>2.2</version>
    </dependency>

	<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.3.3</version>
	</dependency>
//上传的文件(路径名字)
File newFile = new File(stringBuffer.toString(), uuid + filename);
//根据file获取流文件
InputStream inputStream = file.getInputStream();
//根据文件名创建一个新文件
File srcFile = new File(file.getOriginalFilename());
//把流文件转化为新文件
inputStreamToFile(inputStream, srcFile);
inputStream.close();
//使用工具类把新文件copy上传
FileUtils.copyFile(srcFile,newFile);
//上传成功后销毁文件
srcFile.delete();

inputStreamToFile方法:

//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
     try {
         OutputStream os = new FileOutputStream(file);
         int bytesRead = 0;
         byte[] buffer = new byte[8192];
         while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
             os.write(buffer, 0, bytesRead);
         }
         os.close();
         ins.close();
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

2.添加配置解决方案,(未验证,用第一种之后复原代码后又不报错了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值