您好,欢迎访问一九零五行业门户网

java怎么下载Http的内容

1、下载流程
在internet上,我们要下载网站上的某一个资源 ,我们会获得一个ur l(uniformresou rce locator),它是一个服务器资源定位的描述 ,下载的过程经常如下方法:
(1)客户端发起连接请求一个url
(2)服务器解析url,并将指定的资源返回一个输入流给客户
(3)客户端接收输入流,将流中的内容存到文件
2、实例
package com.hu.down; import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.file;import java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.ioexception;import java.net.httpurlconnection;import java.net.malformedurlexception;import java.net.url; public class downfile { public final static boolean debug = true; //调试用    private static int buffer_size = 1024; //缓冲区大小        public void savetofile(string desturl){            bufferedinputstream bis = null;        httpurlconnection httpurl = null;        url url = null;        byte[] buf = new byte[buffer_size];                try {url = new url(desturl);} catch (malformedurlexception e) {// todo auto-generated catch blocksystem.out.println(desturl+资源url语法错误,请检查字符串是否正确!);return;}        try {httpurl = (httpurlconnection) url.openconnection();} catch (ioexception e) {system.out.println(打开到 +desturl+所引用的远程对象的连接失败);}                try {httpurl.connect();} catch (ioexception e) {system.out.println(打开到此 +desturl+ 引用的资源的通信链接失败);return;}                        try {bis = new bufferedinputstream(httpurl.getinputstream());} catch (ioexception e) {system.out.println(取得连接的input流失败);return;}                file file = new file(d:/upload + desturl.substring(desturl.lastindexof(/)));                bufferedoutputstream fileout=null;try {fileout = new bufferedoutputstream(new fileoutputstream(file));} catch (filenotfoundexception e) {system.out.println(file+在本地保存文件失败);e.printstacktrace();}        try{         while (true) {                int bytesin = bis.read(buf, 0, 1024);                if (bytesin == -1) {                   break;                } else {                   fileout.write(buf, 0, bytesin);                }             }                          fileout.flush();             fileout.close();        }catch(exception ee){         system.out.println(file+保存文件过程失败);        }         system.out.println(file.getabsolutepath()+下载完毕);            }public static void main(string[] args) throws ioexception {downfile d=new downfile();string youclass=11003080;string baseurl=http://photo/+youclass;for(int i=301;i<=340;i++){  d.savetofile(baseurl+i+.jpg);}} }
以上就是java怎么下载http的内容的详细内容。
其它类似信息

推荐信息