java-MultipartFile转File

2019-05-18  本文已影响0人  极简_Fengzt

public static File multipartFileToFile(MultipartFile file) throws Exception{

    File toFile =null;

    if(file.equals("")||file.getSize()<=0){

        file =null;

    }else {

        InputStream ins =null;

        ins = file.getInputStream();

        toFile =new File(file.getOriginalFilename());

        inputStreamToFile(ins, toFile);

        ins.close();

    }

    return toFile;

}

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

    }

}

上一篇 下一篇

猜你喜欢

热点阅读