File writeTo File

文件流问题: file 写到另一个 file

    /**
     * 把服务器上源文件写到本项目临时文件夹中;
     * 目的为了在jsp页面在线转换PDF时能获取到url
     * this.getServletConfig().getServletContext().getRealPath("/") 获取项目运行路径
     * @param fjpath 附件相对路径
     * */
    private void writeToProject(String fjpath) {
        String name = null;
        String fileFold = null;
        InputStream is = null;
        FileOutputStream fos = null;
        String lswjj = null;
        try {
            String[] names = fjpath.split("//"); //以斜杠分割,因为保存在附件表中的路径为:/JYGL_ZYJSFJ//2016届毕业简介_1449544857570.pdf
            fileFold = names[0]; //文件夹:/JYGL_ZYJSFJ
            name = names[1]; //文件名:2016届毕业简介_1449544857570.pdf
            lswjj = this.getServletConfig().getServletContext().getRealPath("/")+fileFold; //临时文件夹(自定义路径)
            QyDao qydao = new QyDao(); //企业Dao操作类
            String xtglPath = qydao.findFilePath(); //xtgl_xtsz(系统设置表)中保存的文件夹路径
            File file = new File(xtglPath+fjpath);  //源文件 D:\xgxt\files\JYGL_ZYJSFJ\2016届毕业简介_1449544857570.pdf 
            is = new FileInputStream(file);
            File newFold = new File(lswjj); //新的临时文件夹
            boolean fold = newFold.exists();
            if (fold == false) {
                newFold.mkdirs(); //创建文件夹
            }
            File uploadFile = new File(lswjj + File.separator + name);
            //uploadFile:D:\JDevRuntime1213\system12.1.3.0.41.140521.1008\o.j2ee\drs\JywApp\ViewControllerWebApp.war\JYGL_ZYJSFJ\2016届毕业简介_1449544857570.pdf
            System.out.println("uploadFile:"+uploadFile);
            fos = new FileOutputStream(uploadFile);
            byte[] buffer = new byte[1024];
            int length = buffer.length;
            int size = 0;
            while ((size = is.read(buffer, 0, length)) != -1) {
                fos.write(buffer, 0, size);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

 

你可能感兴趣的:(File writeTo File)