博客
关于我
zip文件上传、解压
阅读量:177 次
发布时间:2019-02-28

本文共 6255 字,大约阅读时间需要 20 分钟。

public synchronized void backUp(HttpServletRequest request, HttpServletResponse response) throws Exception {    String data = "许可无效!";    if (!MapAccessBean.getIsValid()) {        response.setContentType("text/html;charset=UTF-8");        data = "";        System.out.println("key01" + data);        response.getWriter().print(data);        return;    }    if (!("on".equals(request.getParameter("KMLModel")) && "on".equals(request.getParameter("lic")))) {        data = "请勾选需要还原的文件!";        response.setContentType("text/html;charset=UTF-8");        data = "";        response.getWriter().print(data);        return;    }    final String KMLModel = GetRealPath.getRootPath() + File.separator + "KmlModel";    final String images = GetRealPath.getRootPath() + File.separator + "images";    final String layerTree = GetRealPath.getRootPath() + File.separator + "tree.xml";    final String lic = GetRealPath.getRootPath() + File.separator + "sysconfig";    String filePath = tempPath + File.separator + "backUp.zip";    File f = new File(tempPath);    if (!f.exists()) {        f.mkdir();    }    f = new File(filePath);    if (f.exists()) {        f.delete();    }    ZipFile zipFile = new ZipFile(filePath);    ZipParameters parameters = new ZipParameters();    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);    parameters.setEncryptFiles(true);    parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);    parameters.setPassword("!@#$%^&*()");    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);    if ("on".equals(request.getParameter("KMLModel"))) {        f = new File(KMLModel);        if (!f.exists()) {            f.mkdir();        }        zipFile.addFolder(KMLModel, parameters);        zipFile.addFolder(images, parameters);        zipFile.addFile(new File(layerTree), parameters);    }    if ("on".equals(request.getParameter("lic"))) {        f = new File(lic);        String[] filelist = f.list();        if (filelist != null) {            for (String fileName : filelist) {                if (fileName.lastIndexOf(".lic") != -1) {                    zipFile.addFile(new File(lic + fileName), parameters);                }            }        }    }    if (!zipFile.isValidZipFile()) {        throw new ZipException("压缩文件不合法,可能被损坏.");    }    response.reset();    response.setContentType("application/zip");    response.setHeader("Content-Disposition", "attachment; filename=backUp.zip");    try {        java.io.FileInputStream in = new java.io.FileInputStream(filePath);        byte[] b = new byte[512];        ServletOutputStream o = response.getOutputStream();        while (-1 != (bytesRead = in.read(b, 0, b.length))) {            o.write(b, 0, bytesRead);        }        in.close();        o.close();        response.flushBuffer();    } catch (IOException e) {        in.close();        System.out.println(e.getMessage());    } catch (Exception e) {        System.out.println(e.getMessage());    } finally {        // o.close();        // if (o != null) {        // o.close();        // }        // if (in != null) {        // in.close();        // }    }    try {        File f2 = new File(filePath);        if (f2.exists()) {            f2.delete();        }    } catch (Exception e) {        e.printStackTrace();    }}
public ModelAndView restore(HttpServletRequest request, HttpServletResponse response) throws Exception {    ModelAndView modelAndView = new ModelAndView();    modelAndView.setViewName("backUpAndRestore");    File f = new File(tempPath);    System.out.println("tempPath:" + tempPath);    if (!f.exists()) {        f.mkdir();    }    String data = "许可无效!";    FileItemFactory factory = new DiskFileItemFactory();    ServletFileUpload upload = new ServletFileUpload(factory);    HashMap params = new HashMap();    try {        List items = upload.parseRequest(request);        for (Iterator it = items.iterator(); it.hasNext();) {            FileItem fileItem = (FileItem) it.next();            if (fileItem.isFormField()) {                try {                    params.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));                } catch (UnsupportedEncodingException e) {                    e.printStackTrace();                }            } else {                String fileName = fileItem.getName();                if (!fileName.endsWith(".zip")) {                    try {                        throw new Exception(Catalog.getLocalizationForKey("KmlController.ZIPAllowed"));                    } catch (Exception e) {                        e.printStackTrace();                    }                }                File file = new File(tempPath, "restore.zip");                try {                    fileItem.write(file);                } catch (Exception e1) {                    e1.printStackTrace();                }                ZipFile zipFile = new ZipFile(file);                if (!zipFile.isValidZipFile()) {                    try {                        throw new ZipException("压缩文件不合法,可能被损坏.");                    } catch (ZipException e) {                        data = "1";                        modelAndView.addObject("message", data);                        return modelAndView;                    }                }                if (zipFile.isEncrypted()) {                    zipFile.setPassword("!@#$%^&*()");                    zipFile.extractAll(GetRealPath.getRootPath());                } else {                    data = "2";                    modelAndView.addObject("message", data);                    return modelAndView;                }            }        }    } catch (FileUploadException e1) {        e1.printStackTrace();    }    if (!MapAccessBean.getIsValid()) {        data = "4";        modelAndView.addObject("message", data);        return modelAndView;    } else {        modelAndView.addObject("message", Catalog.getLocalizationForKey("KmlController.restorSuccess"));        if ("on".equals(params.get("checkURL"))) {            try {                kmlService.checkIconUrl(request.getServerName() + ":" + request.getServerPort());            } catch (Exception e) {                e.printStackTrace();            }        }        SysConfig.getInstance().init();    }    return modelAndView;}

转载地址:http://dwvj.baihongyu.com/

你可能感兴趣的文章
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>