上传本地压缩包及解压ZIP

之前一直都是做一些片段没有整体做过,因为项目需要所以在此记录下。。。

     @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 拼接路径(根据自己路径拼接)
        String savePath = request
                .getSession()
                .getServletContext()
                .getRealPath(
                        "upload" + System.getProperty("file.separator")
                                + "Batch");
        File filed = new File(savePath);
        // 判断目录是否存在
        if (!filed.exists()) {
            filed.mkdirs();
        }
        // 创建工厂类
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 上传本地服务器
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 防止中文乱码
        upload.setHeaderEncoding("utf-8");
        List<FileItem> fileList = null;
        try {
            // 调用插件方法获取文件
            fileList = upload.parseRequest(request);
        } catch (Exception e) {
            return;
        }
        Map<String, Object> mapFile = new HashMap<String, Object>();
        Map mapLocalFile = new HashMap();
        // 迭代获取文件信息
        Iterator<FileItem> itFime = fileList.iterator();
        String name = "";
        String exName = "";
        while (itFime.hasNext()) {
            FileItem fileItem = itFime.next();
            if (!fileItem.isFormField()) {
                name = fileItem.getName();
                long size = fileItem.getSize();
                String type = fileItem.getContentType();
                // System.out.println(size + " " + type);
                // 拓展名格式
                if (name.lastIndexOf(".") >= 0) {
                    exName = name.substring(name.lastIndexOf("."));
                    // System.out.println(exName);
                }
                // 根据路径解压zip
                if (name == null || name.trim().equals("")) {
                    continue;
                }
                File file = null;
                do {
                    // 生成文件名:
                    file = new File(savePath
                            + System.getProperty("file.separator") + name);
                } while (file.exists());
                File saveFile = new File(savePath
                        + System.getProperty("file.separator") + name);
                try {
                    fileItem.write(saveFile);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }

            response.getWriter().print(name);

if(!fileItem.isFormField()){
                try {
                    this.decompressZip(mapFile,request);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /**
     * @deprecated 解压文件
     * @param zipFilePath
     *            zip 文件路径
     * @param descDir
     *            解压出来的文件保存目录
     * @throws IOException
     */
    public void decompressZip(Map<String, Object> map,
            HttpServletRequest request) throws Exception, Exception {
        // 获取tomcat下路径文件
        // String file =
        // map.get("savePath").toString()+System.getProperty("file.separator")+
        // map.get("nameZip").toString();
        File zipFile = new File(map.get("savePath").toString()
                + System.getProperty("file.separator")
                + map.get("nameZip").toString());
        File pathFile = new File(map.get("savePath").toString()
                + System.getProperty("file.separator") + "ZIP");
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        ZipFile zip = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            System.out.println("==================解压开始==================");
            Map duailFile = new HashMap();
            long startTimes = System.currentTimeMillis();
            zip = new ZipFile(zipFile);
            Enumeration<?> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                long size = entry.getSize();
                in = zip.getInputStream(entry);
                String outPath = pathFile + File.separator + zipEntryName;
                // 判断路径是否存在,不存在则创建文件路径
                File file = new File(outPath.substring(0,
                        outPath.lastIndexOf("/")));
                if (!file.exists()) {
                    file.mkdirs();
                }
                // 判断文件全路径是否为文件夹,如果是上面已经创建,不需要解压
                if (new File(outPath).isDirectory()) {
                    continue;
                }
                out = new FileOutputStream(outPath);
                   String biz_id = map.get("bizId").toString();
                byte[] buf = new byte[4 * 1024];
                int len;
                while ((len = in.read(buf)) >= 0) {
                    out.write(buf, 0, len);
                }
              
                in.close();
                System.out.println("==================解压完毕==================");
                long times = System.currentTimeMillis() - startTimes;
                System.out.println("耗时:" + times / 1000 + "秒");
            }
        } catch (Exception e) {
            System.out.println("==================解压失败==================");
            conn.deletefile(map.get("savePath").toString());
            e.printStackTrace();
        } finally {
            try {
                if (zip != null) {
                    zip.close();
                }
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值