spring里面的CommonsMultipartFile如何转File呢?(强转是行不通的,观看源码得知利用InputStream转换)具体实现代码如下:
private static void inpuStreamToFile(InputStream ins,File file) {
OutputStream oStream = null;
try {
oStream = new FileOutputStream(file);
int bytesRead = 0;
byte[]buffer = new byte[1024];
while ((bytesRead = ins.read(buffer))!=-1) {
oStream.write(buffer,0,bytesRead);
}
} catch (Exception e) {
throw new RuntimeException("调用inputStreamToFile产生异常:"+e.getMessage());
}finally {
try {
if (oStream != null) {
oStream.close();
}
if (ins != null) {
ins.close();
}
} catch (IOException e) {
throw new RuntimeException("inputStreamToFile关闭io流产生异常:"+e.getMessage());
}
}
}