作用:不用每次调用private static void inputStreamToFile(InputStream ins,File file) 和创建新的文件,即下面这个函数可以去掉。
/**
* 把InputStream转为File
* @param ins
* @param file
*/
private static void inputStreamToFile(InputStream ins,File file) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while((bytesRead = ins.read(buffer)) != -1) {
os.write(buffer,0,bytesRead);
}
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("调用inputStreamToFile产生异常"+e.getMessage());
}finally {
try {
if(os != null) {
os.close();
}
if(ins != null) {
ins.close();
}
} catch (IOException e2) {
// TODO: handle exception
throw new RuntimeException("调用inputStreamToFile关闭io参数异常"+e2.getMessage());
}
}
直接这样就行了:
se = shopService.addShop(shop, shopImg.getInputStream(),shopImg.getOriginalFilename());