急!给出一个URI,对应的是服务端的的一个zip包,客户端如何用java对它进行下载,
已知的有:Stringpath=uri.toSting()=http://127.0.0.1:8181/add_manage/js_home/890321.zip实例化f...
已知的有:String path =uri.toSting()=http://127.0.0.1:8181/add_manage/js_home/890321.zip
实例化file:File file = new File(uri)--这个是错误的,因为file.exists()=false,
我现在只能就是把这个zip包下载下来,然后实例化,再解压,可是我现在不知道用什么方法下载呢,求教一下,另外服务器与客户端的通信我使用的是httpclient
感谢2楼大哥!这个我写的完成了!(*^__^*)
public boolean zipDl(String ver,URL url,String rootPath) {
// TODO Auto-generated method stub
boolean a = false;
BufferedInputStream bis;
FileOutputStream fos;
try {File file = new File(rootPath+"zip_home");
if(delete(file)){
File target = new File(file,ver+".zip");
target.createNewFile();
System.out.println(target.exists());
System.out.println(url.toString());
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的网络资源
httpUrl.connect();
//获取网络输入流bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while((len=bis.read(buf))!=-1)
fos.write(buf, 0, len);
a = true;}} catch (IOException e) {e.printStackTrace();}return a;}
private boolean delete(File dir) {boolean b = false;
File[] files = dir.listFiles();
for(File file : files){
if(file.isDirectory()){
delete(file);}
file.delete();
b = true;}
return b;} 展开
实例化file:File file = new File(uri)--这个是错误的,因为file.exists()=false,
我现在只能就是把这个zip包下载下来,然后实例化,再解压,可是我现在不知道用什么方法下载呢,求教一下,另外服务器与客户端的通信我使用的是httpclient
感谢2楼大哥!这个我写的完成了!(*^__^*)
public boolean zipDl(String ver,URL url,String rootPath) {
// TODO Auto-generated method stub
boolean a = false;
BufferedInputStream bis;
FileOutputStream fos;
try {File file = new File(rootPath+"zip_home");
if(delete(file)){
File target = new File(file,ver+".zip");
target.createNewFile();
System.out.println(target.exists());
System.out.println(url.toString());
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的网络资源
httpUrl.connect();
//获取网络输入流bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while((len=bis.read(buf))!=-1)
fos.write(buf, 0, len);
a = true;}} catch (IOException e) {e.printStackTrace();}return a;}
private boolean delete(File dir) {boolean b = false;
File[] files = dir.listFiles();
for(File file : files){
if(file.isDirectory()){
delete(file);}
file.delete();
b = true;}
return b;} 展开
2个回答
展开全部
1:获取一个URL地址
2:解析URL,返回一个输入流
3:客户端接收,将流中的内容保存
//获取url并解析
Url url = null;
destUrl="http://127.0.0.1:8181/add_manage/js_home/890321.zip";//此处是http地址
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的网络资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
//保存资源
fos = new FileOutputStream(fileName);
if (this.DEBUG)
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName +"]");
//保存文件
while ( (size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
//到此就可以实现下载的功能了!
2:解析URL,返回一个输入流
3:客户端接收,将流中的内容保存
//获取url并解析
Url url = null;
destUrl="http://127.0.0.1:8181/add_manage/js_home/890321.zip";//此处是http地址
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的网络资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
//保存资源
fos = new FileOutputStream(fileName);
if (this.DEBUG)
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName +"]");
//保存文件
while ( (size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
//到此就可以实现下载的功能了!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询