MultipartFile转File

工具类

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class PoiTools {
    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();
          }
         }
}

控制类使用

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

@RequestMapping(value = "/importinventory.form", produces = "text/html;charset=utf-8") @ResponseBody public String addTemplet( HttpServletRequest request, @RequestParam("file") MultipartFile file, HttpSession session) throws IOException { File f = null; if(file.equals("")||file.getSize()<=0){ file = null; }else{ InputStream ins = file.getInputStream(); f=new File(file.getOriginalFilename()); PoiTools.inputStreamToFile(ins, f); } System.out.print("MultipartFile file转File f,f="+f);
posted @ 2018-11-09 09:56  LOGAN彡  阅读(1837)  评论(0编辑  收藏  举报