问题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();
}
}