zip文件下载

    /**
     * 从srcUrlStr下载zip文件保存到descFilePath路径下
     * @param srcUrlStr
     * @param descFilePath
     * @param timeout
     * @return Boolean true:ok flase:Error
     */
    public static Boolean DownloadZip(String srcUrlStr, String descFilePath,
            int timeout) throws IOException {
       
        URL url = null;
        URLConnection urlcon = null;
        ZipInputStream zipis = null;
        ZipOutputStream zipos = null;

        try {
            url = new URL(srcUrlStr);
            urlcon = url.openConnection();
            int length = urlcon.getContentLength();
            urlcon.setReadTimeout (timeout);
            zipis = new ZipInputStream(urlcon.getInputStream());
            ZipEntry zipEntry = zipis.getNextEntry();

            File f = new File(descFilePath);
            zipos = new ZipOutputStream(new FileOutputStream(f));
            zipos.setMethod(ZipOutputStream.DEFLATED);
            zipos.putNextEntry(zipEntry);

            int len = (length > 100000) ? 100000 : length;
            byte[] bArray = new byte[len];
            int retVal = 0;
            while ((retVal = zipis.read(bArray, 0, len)) != -1) {
                zipos.write(bArray, 0, retVal);
            }
            zipis.close();
            zipos.flush ();
            zipos.close();
            return true;
        } catch (IOException ioe) {
            System.out.println("DownloadZip Err " + ioe.getMessage());
            return false;
        } finally {
            if (null != zipis) {
                zipis.close();
            }
            if (null != zipos) {
                zipos.close();
            }
        }
    }



--
Blog:     www.borderj.cn

  Border
评论
发表评论

您还没有登录,请登录后发表评论

border
搜索本博客
存档
最新评论