此处用曲线救国的方式:先用CommonsMultipartFile的getInputStream()方法获取到一个InputStream类型的对象,再将InputStream类型转化为File
//将InputStream类型转化为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) { throw new RuntimeException("调用inputStreamToFile产生异常:"+e.getMessage()); }finally { try { if(os != null) { os.close(); } if(ins != null) { ins.close(); } }catch(IOException e) { throw new RuntimeException("inputStreamToFile关闭io时产生异常:"+e.getMessage()); } } } //使用inputStreamToFile方法 //创建文件时,随便加一个空路径,将文件创建出来,后续再删除即可 File xxFile = new File("一个不重复的路径"); try { xxFile.createNewFile(); } catch (IOException e) { //处理异常信息 } try { inputStreamToFile(xxCommonsMultipartFile.getInputStream(), xxFile); } catch (IOException e) { //处理异常信息 } //xxFile就是转化后的File文件