1. 程式人生 > >MultipartFile轉File

MultipartFile轉File

new bytes ddt lena java [] tst read input

工具類

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);

MultipartFile轉File