2007-09-06
『Java 』zip文件下载
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
发表评论
- 浏览: 22735 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
『Linux』如何在Gaim中使 ...
<a target=blank href=tencent://messag ...
-- by loveanight -
dom4j解析特殊字符出错
还以为CDATA可以躲过一截,结果还是报错。。。 org.dom4j.Do ...
-- by border -
如何通过Java调用Servlet ...
urlCach = new URL("http://192.168.0 ...
-- by border -
如何通过Java调用Servlet ...
其实我就是不明白为什么URLConnection有的时候可以,但是有的时候不能。
-- by border -
如何通过Java调用Servlet ...
[jlusdy 写道这么简单可以么? 还是用HttpClient吧 本人从来 ...
-- by 超级莱鸟






评论排行榜