springboot多文件上传问题

假装没事ソ 提交于 2019-12-28 05:20:54

问题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.添加配置解决方案,(未验证,用第一种之后复原代码后又不报错了)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!