public String infornationshow(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IOException{
File f = null;
if(file.equals("")||file.getSize()<=0){
file = null;
}else{
InputStream ins = file.getInputStream();
f=new File(file.getOriginalFilename());
inputStreamToFile(ins, f);
}
public 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();
}
}
但这种办法会出现一个问题,会在项目目录下自动生成一个临时的file文件,如何将这个文件删除呢?
File del = new File(f.toURI());
del.delete();