Welcome to Yumao′s Blog.
Java簡易文檔下載器
, 2013年03月18日 , Java Language , 评论 在〈Java簡易文檔下載器〉中留言功能已關閉 ,

使用HttpClient將文件抓到內存byte數組內
然後進行文件的buff輸出
亦或者可以直接在內存中進行二次轉送
以達到各種目的
本例子只給出基本代碼
優化等其他操作請自行嘗試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
 
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;
import org.apache.http.util.EntityUtils;
 
 
public class downTest {
    private static final String GET_URL = "http://static-img1.cdn.aliyuncs.com/images/aliyun/404_ico.png";
    private static final String FILE_PATH = "/root/down.png";
    public static void main(String[] args) throws Exception {
        HttpClient hc = new DefaultHttpClient();
        HttpGet get = new HttpGet(GET_URL);
        HttpResponse response = hc.execute(get);
        if (response.getStatusLine().getStatusCode() == 200) {
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            FileOutputStream outSTr = new FileOutputStream(new File(FILE_PATH));
            BufferedOutputStream Buff = new BufferedOutputStream(outSTr);
            Buff.write(bytes);
            Buff.flush();
            Buff.close();
            System.out.println("Done!");
        }
    }
}
关键字:, , ,

评论已关闭