组合ext实现文件的上传和下载 实现断点续传

结合ext实现文件的上传和下载 实现断点续传
chrome浏览器,前端ExtJS, 控制层为struts2

/**
* Created with IntelliJ IDEA.
* User: livgo
* Date:
* Time:
* 文件上传 与 下载
*/
public class UpDownloadAction{
    private Logger log = LoggerFactory.getLogger(UpDownloadAction.class);
    private File file;        //上传的文件
    private String[] paths;   //下载文件路径
    private String fileName;  //文件名
    private Long fileSize; //文件大小

    /**
     * 上传文件判断
     */
    public void loadFileTurn() {
        if (log.isDebugEnabled())
            log.debug("[文件上传与下载/loadFileTurn()]:" + fileName + ":" + fileSize);
        String json = "{success:true, flag:'jx'}";   //文件可以正常上传

        //上传路径   config 自己配去 我不贴了
        String upLoadPath = getBean("upload", UpLoadConfig.class).getUploadPath();
        // File upFile = new File(ServletActionContext.getServletContext().getRealPath(upLoadPath), fileName);
        File upFile = new File(upLoadPath, fileName);
        if (upFile.exists()) {  //此文件是否已传完
            json = "{success:true, flag:'fg', msg:'文件名重复,是否覆盖?是-覆盖 否-重新选择文件'}";
        } else {
            File upTempFile = new File(upLoadPath + "/temp", fileName);
            if (upTempFile.exists()) { //是否有未传完的临时文件
                if (upTempFile.length() < fileSize) {
                    json = "{success:true,flag:'xc', msg:'文件未传完,是否续传?是-续传 否-重新传'}";
                } else {
                    upTempFile.delete();
                }
            }
        }
       // 自己实现返回json
        if (log.isDebugEnabled())
            log.debug("/文件上传 与 下载//uploadFile////////////////////:" + json);
    }

    /**
     * 空方法 辅助业务流转
     */
    public void blankMethod() {
    // 自己实现返回json 成功
      
    }

    /**
     * 文件上传
     */
    public void uploadFile() {
        uploadFile(false);
    }

    /**
     * 文件上传
     */
    public void uploadFile(boolean flag) {

        if (log.isDebugEnabled())
            log.debug("[文件上传与下载/uploadFile()]:" + fileName);
        String json = "{success:true}";
        if (null != file) {
            //上传路径
            String upLoadPath = getBean("upload", UpLoadConfig.class).getUploadPath();
            File upTempFile = new File(upLoadPath + "/temp", fileName);
            if (!upTempFile.exists()) {
                try {
                    upTempFile.getParentFile().mkdirs();//没有temp文件就创建一个
                    upTempFile.createNewFile();
                } catch (IOException e) {
                    json = "{success:false, flag:'', msg:'文件创建失败,请联系管理员!'}";
                    e.printStackTrace();
                }
            }
            //flag 覆盖、正常传-false  续传-true
            File tmpFile = readWrite(file, upTempFile, flag);
            if (tmpFile != null) {
                File fl = new File(upLoadPath, fileName);
                tmpFile.renameTo(fl);//将临时文件删除 并将实际文件保存
            }
        } else {
            json = "{success:false, flag:'', msg:'文件不可为空'}";
        }
        // 自己实现返回json
        if (log.isDebugEnabled())
            log.debug("/文件上传 与 下载//uploadFile////////////////////:" + json);
    }

    /**
     * 读写文件
     */
    private File readWrite(File rFile, File wFile, boolean flag) {
        if (log.isDebugEnabled())
            log.debug("[文件上传与下载/readWrite()]:" + fileName);
        RandomAccessFile isRaf = null;
        RandomAccessFile osRaf = null;
        int fg = 0;
        try {
            isRaf = new RandomAccessFile(rFile, "rw");
            osRaf = new RandomAccessFile(wFile, "rw");

            byte[] buffer = new byte[1024];
            if (flag) { //续传
                isRaf.seek(wFile.length());
                osRaf.seek(wFile.length());
            }
            int len = 0;
            while ((len = isRaf.read(buffer)) > 0) {
                osRaf.write(buffer, 0, len);
            }
        } catch (Exception e) {
            fg = 1;
            log.error("操作异常", e);
           
        } finally {
            if (osRaf != null) {
                try {
                    osRaf.close();
                } catch (IOException e) {
                    fg = 1;
                    e.printStackTrace();
                }
            }
            if (isRaf != null) {
                try {
                    isRaf.close();
                } catch (IOException e) {
                    fg = 1;
                    e.printStackTrace();
                }
            }
        }
        if (fg == 0) return wFile;
        else return null;
    }

    /**
     * 覆盖文件
     */
    public void coverFile() {
        String upLoadPath = getBean("upload", UpLoadConfig.class).getUploadPath();
        File upFile = new File(upLoadPath, fileName);
        if (upFile.delete()) {
            uploadFile(false);
        } else  // 自己实现返回json
    }

    /**
     * 续传文件or重新传
     */
    public void continueUpload() {
        String goon = getRequest().getParameter("goon");
        if (StringUtils.equals("y", goon)) uploadFile(true);
        else if (StringUtils.equals("n", goon)) uploadFile(false);
    }

    /**
     * 文件下载
     */
    public void downloadFile() {
        if (log.isDebugEnabled())
            log.debug("[文件上传与下载/downloadFile()]:" + paths);
        String json = "";
        StringBuffer strb = new StringBuffer();
        if (paths != null && paths.length > 0) {
            String uploadPath = getBean("upload", UpLoadConfig.class).getUploadPath();
            String downloadPath = getBean("upload", UpLoadConfig.class).getDownloadPath();
//            String downloadPath = getRequest().getParameter("downloadPath");     //指定路径下载
            for (int i = 0; i < paths.length; i++) {
                String fileName = paths[i].substring(paths[i].lastIndexOf("/") + 1);
                File upTempFile = new File(downloadPath + "/temp", fileName);
                if (!upTempFile.exists()) {
                    try {
                        upTempFile.getParentFile().mkdirs();//没有temp文件就创建一个
                        upTempFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
//                File uploadFile = new File(ServletActionContext.getServletContext().getRealPath(uploadPath), fileName);
                File uploadFile = new File(uploadPath, fileName);    //目录可能会改动,所以不直接 new 表里存的文件路径
                File tmpFile = null;
                //续传
                if (upTempFile.exists()) {
                    tmpFile = readWrite(uploadFile, upTempFile, true);
                } else { //直接写
                    tmpFile = readWrite(uploadFile, upTempFile, false);
                }
                if (tmpFile != null) {//续传
                    File fl = new File(downloadPath, fileName);
                    tmpFile.renameTo(fl);//将临时文件删除 并将实际文件保存
                }
            }
            if (strb.length() == 0) {
                json = "{success:true, msg:'下载成功,文件保存在" + downloadPath + "'}";
            } else {
                json = "{success:true, msg:'" + strb.toString() + "找不到文件,下载失败,其它文件保存在" + downloadPath + "'}";
            }
        } else {
            json = "{success:true, msg:'请选择需要下载的文件'}";
        }
        // 自己实现返回json
        if (log.isDebugEnabled())
            log.debug("/文件上传 与 下载//downloadFile////////////////////:" + json);
    }

}