public static String uploadToTmp(MultipartFile file) throws IOException {
//时间戳
String tim = String.valueOf ( System.currentTimeMillis () );
//临时路径存储临时文件
String tmpPath = "/fileUpload/tmp/"+tim+"/";
File tmpdir = new File ( tmpPath );
if (!tmpdir.exists ()) {
tmpdir.mkdirs ();
}
//临时文件的全路径
String tmpUrl = tmpPath + file.getOriginalFilename();
File tmptFile = new File ( tmpUrl );
FileOutputStream fs=new FileOutputStream(tmptFile);
InputStream stream = file.getInputStream();
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
return tmpUrl;
}
网友评论