栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用Java处理下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Java处理下载

假设您实际上在谈论HttpClient,这是一个SSCCE:

package com.stackoverflow.q2633002;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;public class Test {    public static void main(String... args) throws IOException {        System.out.println("Connecting...");        HttpClient client = new DefaultHttpClient();        HttpGet get = new HttpGet("http://apache.cyberuse.com/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin.zip");        HttpResponse response = client.execute(get);        InputStream input = null;        OutputStream output = null;        byte[] buffer = new byte[1024];        try { System.out.println("Downloading file..."); input = response.getEntity().getContent(); output = new FileOutputStream("/tmp/httpcomponents-client-4.0.1-bin.zip"); for (int length; (length = input.read(buffer)) > 0;) {     output.write(buffer, 0, length); } System.out.println("File successfully downloaded!");        } finally { if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}        }    }}

在这里工作正常。您的问题出在其他地方。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470374.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号