压缩下载文件

 
链接信息前台压缩下载后台的图片

       function downloadZipFill() {
 window.location = encodeURI("${path}/trans/tower_downloadZipFill.do");
     $.showLoad();
     $.messager.show({
                 title:"消息提醒",
                 msg:"正在下载,请稍后...",
                 timeout:5000,
                 showType:"slide"
             });
  return false;
}

 

 function download(){

            var selections=$("#table").datagrid("getSelections");
               var tag = true;
            if(selections.length==0){
                $.messager.alert("系统提示", "请选择需要下载的记录,同时确定记录下存在二维码!", "info");
            }else{
                $.messager.confirm("系统提示", "确认并下载?", function(b){
                    if(b){
                        $.showLoad();
                         var tt_ids=new Array();
                        // var ccj_status_array = new Array();


                          $.each(selections, function (i, o) {
                             tt_ids.push(o.tt_id)
                            });


                        if(tag){
                        $.ajaxPost("${path}${path}/trans/tower_downloadFills.do", {"tt_ids":tt_ids}, function(result) {
                            if(!downloadZipFill()){
                                 $.hideLoad();
                            }
                            if("0"!=result){
                                $.messager.show({
                                            title:"消息提醒",
                                            msg:"下载成功!",
                                            timeout:5000,
                                            showType:"slide"
                                        });


                            } else{
                                alert("选择项无二维码,请生成后下载!") ;
                            }
                        });
                            }
                    }
                });
            }
        }
 

 


/**
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    String zipPath = "";


    @ActionAnnotation(name = "压缩下载", group = "下载", log = true)
    public ModelAndView downloadFills(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] tt_ids = BeanUtil.wrapArray(String.class, request.getParameter("tt_ids"));
        zipPath = towerService.downloadFills(tt_ids);
        String result = zipPath;
        return responseText(response, result);
    }


    /**
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ActionAnnotation(name = "下载附件", group = "下载", log = true)
    public ModelAndView downloadZipFill(HttpServletRequest request, HttpServletResponse response) throws Exception {
        FileDownLoad fileDownLoad = new FileDownLoad(response);
        fileDownLoad.write(zipPath);
        response.flushBuffer();
        towerService.deleteFile();  //下载后删除文件
        return null;
    }
/**
     * 得到附件
     *
     * @param sf_id
     * @return
     * @throws Exception
     */
    public Files getFj(String sf_id) throws Exception {
        return sqlDao.query("sys_file.queryBlob", new Files("sf_id", sf_id));
    }


    String zipPath = "";


    public boolean makeDirs(String[] tt_ids) throws Exception {
        zipPath = "杆塔二维码";  //使用日期作为压缩文件的文件名
        ServletContext context = getServletContext();
        zipPath = context.getRealPath("") + "\\" + zipPath + "\\" + "code" + DateUtil.timeToStr2(new Date());  //定义path  与盘符无关
        createDir(zipPath); //在c盘创建相应文件目录


        for (String tt_id : tt_ids) {
            Tower tower = query(tt_id);
            if (!StringUtil.isEmpty(tower) && !StringUtil.isEmpty(tower.getTt_picurl())) {
                File t = createFile(zipPath + "\\" + tower.getTt_picurl());
                File pic = new File(servletContext.getRealPath("/ECODE/" + tower.getTt_picurl()));
                if (pic.exists() && pic.isFile() && t.exists() && t.isFile()) {
                    FileUtil.copy(pic, t);    //将图片拷贝到path路径下
                }
            }


        }


        return true;
    }


    /**
     * @param tt_ids
     * @return
     * @throws Exception
     */
    public String downloadFills(String[] tt_ids) throws Exception {
        if (!StringUtil.isEmpty(tt_ids)) {
            makeDirs(tt_ids);
        }
        if (!"".equals(zipPath)) {
            zip(zipPath, zipPath + ".zip");
            return zipPath + ".zip";
        } else {
            return null;
        }
    }
    //----------------文件操作


    /**
     * 创建文件夹
     *
     * @param path
     * @return
     */
    private File createDir(String path) {
        File dirFile = null;
        try {
            dirFile = new File(path);
            if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
                dirFile.mkdirs(); //创建文件夹


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dirFile;
    }


    /**
     * 创建文件
     *
     * @param path
     * @return
     */
    private File createFile(String path) {
        File file = new File(path);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    // 删除文件


    /**
     * @param path
     * @return
     * @throws Exception
     */
    public boolean delFile(String path) throws Exception {
        boolean result = false;
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            file.delete();
            result = true;
        }
        return result;
    }   // 删除文件及文件夹


    /**
     * @param folder
     * @return
     */
    private boolean delDir(File folder) {
        boolean result = false;
        try {
            String childs[] = folder.list();
            if (childs == null || childs.length <= 0) {
                if (folder.delete()) {
                    result = true;
                }
            } else {
                for (int i = 0; i < childs.length; i++) {
                    String childName = childs[i];
                    String childPath = folder.getPath() + File.separator
                            + childName;
                    File filePath = new File(childPath);
                    if (filePath.exists() && filePath.isFile()) {
                        if (filePath.delete()) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    } else if (filePath.exists() && filePath.isDirectory()) {
                        if (delDir(filePath)) {
                            result = true;
                        } else {
                            result = false;
                            break;
                        }
                    }
                }
            }
            folder.delete();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }


    public void deleteFile() {
        if (!StringUtil.isEmpty(zipPath)) {

            deleteFile(zipPath);

             deleteFile(zipPath+".zip");

        }
        zipPath = "";//删除后将文件赋空值
    }


    /**
     * 删除文件、文件夹
     */
    public void deleteFile(String path) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] ff = file.listFiles();
            for (int i = 0; i < ff.length; i++) {
                deleteFile(ff[i].getPath());
            }
        }
        file.delete();
    }


    public void zip(String inputFileName, String zipFileName) throws Exception {
        System.out.println(zipFileName);
        zip(zipFileName, new File(inputFileName));
    }


    private void zip(String zipFileName, File inputFile) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFileName));
        out.setEncoding("gbk");
        zip(out, inputFile, "");
        System.out.println("zip done");
        out.close();
    }


    private void zip(ZipOutputStream out, File f, String base) throws Exception {
        if (f.isDirectory()) {  //判断是否为目录
            File[] fl = f.listFiles();
//            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (int i = 0; i < fl.length; i++) {
                zip(out, fl[i], base + fl[i].getName());
            }
        } else {                //压缩目录中的所有文件
//            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
            out.putNextEntry(new ZipEntry(base));
            FileInputStream in = new FileInputStream(f);
            int b;
            System.out.println(base);
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
        }
    }
FileUtil:

 

    public static void copy(File s,File t){
         InputStream fis=null;
         OutputStream fos=null;
         try {
            fis=new BufferedInputStream(new FileInputStream(s));
             fos=new BufferedOutputStream(new FileOutputStream(t));
             byte[] bf=new byte[4096];
             int i;
             try {
                while((i=fis.read(bf)) != -1){
                     fos.write(bf, 0, i);
                 }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try{
                    fis.close();
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
     }

 

欢迎关注我的公众号,会有不一样的惊喜。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jimin_zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值