1. 程式人生 > >File存本地

File存本地

    public File savePic(File file) {
        File fileDir = new File("D:\\marTemp");
        InputStream inputStream = null;
        OutputStream os = null;
        if(!fileDir.exists()){
            System.out.println("mkdir");
            fileDir.mkdir();
        }

        try {
            byte[] bs = new byte[1024 * 1024 * 5];//1k * 1024 * 5
            int len;
            inputStream = new FileInputStream(file);
            logger.info("inputStream:"+inputStream);
            os = new FileOutputStream(fileDir.getPath()+File.separator+file.getName());
            logger.info("os:"+os);
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.info("FileNotFoundException:--------"+e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("IOException:--------"+e.getMessage());
        }finally {
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                logger.info("FinallyIOException:--------"+e.getMessage());
            }
        }
        File sendFile = new File(fileDir.getPath()+File.separator+file.getName());
        return sendFile;
    }