FileChannel复制文件

使用FileChannel复制文件的速度比BufferedInputStream/BufferedOutputStream复制文件的速度快。而且FileChannel是线程安全的。

/**
 * 复制文件,使用文件通道的方式
 * @param sourceFile
 * @param destFile
 */
public void fileChannelCopy(File sourceFile, File destFile) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel in = null;
    FileChannel out = null;
    try {
        fis = new FileInputStream(sourceFile);
        fos = new FileOutputStream(destFile);
        in = fis.getChannel();
        out = fos.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
            in.close();
            fos.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

创建文件夹

String dirStr = JSFUtils.getInitParameter("xspic_path") + "temp";
//web.xml配置的context-param参数,值为/jwxt/picture/xspic/
File directory = new File(dirStr);
if (directory.exists()) {
    this.removeDir(directory); //如果目录存在,则先删除再创建
}
directory.mkdirs();

/**
 * 删除文件夹下所有文件,递归
 * @param dir
 * @return
 */
private boolean removeDir(File dir) {
    if (dir.isDirectory()) {
        String[] fileList = dir.list();
        for (String file : fileList) {
            boolean flag = removeDir(new File(dir, file));
            if (!flag) {
                return false;
            }
        }
    }
    return dir.delete(); //此时目录为空,可以删除
}

业务代码

/**
 * 批量下载
 * @param facesContext
 * @param outputStream
 */
public void downloadAll(FacesContext facesContext,
                        OutputStream outputStream) {
    String dirStr = JSFUtils.getInitParameter("xspic_path") + "temp";
    //web.xml配置的context-param参数,值为/jwxt/picture/xspic/
    File directory = new File(dirStr);
    if (directory.exists()) {
        this.removeDir(directory); //如果目录存在,则先删除再创建
    }
    directory.mkdirs(); //创建目录
    String path = null;
    for (Row row : this.xszpxxValue) {
        this.fileName = (String)row.getAttribute("Xm") + row.getAttribute("Sfzjh") + ".jpg";
        path = (String)(row.getAttribute("Imagepath") == null ? "" : row.getAttribute("Imagepath"));
        path = path.substring(0, path.lastIndexOf("/")) + ".jpg";
        File sourceFile = new File(path);
        File destFile = new File(directory, fileName);
        if (sourceFile.exists()) {
            this.fileCopy(sourceFile, destFile);
        }
    }
    String zipFilePath = JSFUtils.getInitParameter("xspic_path") + File.separator + jxbmc + ".zip";
    CHZipUtils.zip(dirStr, zipFilePath); //打包成zip格式
    //this.zipFileName = jxbmc + "zip"; //教学班名称作为下载文件名
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(new File(zipFilePath));
        IOUtils.copy(fis, outputStream);
        outputStream.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

转载于:https://my.oschina.net/u/3646781/blog/1607885

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值