import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;public class Downloader extends Thread {
private static final String filepath = "D:/";// 文件存放路径
private static final int BUFFER_SIZE = 1000;// byte数组的大小
private int fileSize;// 文件的大小
private int bytesRead;// 已经读取的字节数
private InputStream inputStream;// 字节输入流的所有类的超类,从网络上读取
private OutputStream outputStream;// 字节输出流的所有类的超类,写入到文件中
private byte[] buffer;// 缓冲区数组 buffer中 public Downloader(String site) throws IOException {
URL downloadURL = new URL(site);
bytesRead = 0;
// HttpURLConnection urlConnection =
// (HttpURLConnection)downloadURL.openConnection();
// URLConnection构造一个到指定 URL 的 URL 连接。
URLConnection urlConnection = downloadURL.openConnection();
urlConnection.connect();
fileSize = urlConnection.getContentLength();// 文件长度
if (fileSize == -1) {
System.out.println("fileSize: " + fileSize);
throw new FileNotFoundException(downloadURL.toString());
}
int index = site.lastIndexOf('/');
File file = new File(filepath + site.substring(index + 1));// 文件名
outputStream = new FileOutputStream(file);
// 在创建 BufferedInputStream 时,会创建一个内部缓冲区数组。
inputStream = new BufferedInputStream(urlConnection.getInputStream());
buffer = new byte[BUFFER_SIZE];
} public void run() {
performDownload();
} /**
 * 负责执行下载的方法。
 */
private void performDownload() {
int byteCount;
while (bytesRead < fileSize) {
try {
// 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 buffer中
// 以整数形式返回实际读取的字节数。存储在缓冲区整数 byteCount中。
byteCount = inputStream.read(buffer);
System.out.println(bytesRead + " and" + fileSize);
// 写入
outputStream.write(buffer, 0, byteCount);
bytesRead += byteCount;
} catch (IOException e) {
break;
}
}
// 关闭流,断开与所下载文件的连接
try {
inputStream.close();
} catch (IOException e) {
System.out.println("inputStream.close()时出现异常");
}
try {
outputStream.close();
} catch (IOException e) {
System.out.println("outputStream.close()时出现异常");
}
} public static void main(String arg[]) {
try {
new Downloader("http://www.developingteam.com/downA").start();
} catch (IOException e) {
System.out.println("main Downloader");
}
}
}我想用这个程序下载http://www.developingteam.com/downA(用浏览器直接打开这个网址,可以下载到那个资源)里的资源。
我查了下他的HTTP报头(URLConnection里的getHeaderFields()方法):所要下载apk的是通过Content-disposition以附件(attachment)的形式附加在http://www.developingteam.com/downA网址上的。这使得
fileSize = urlConnection.getContentLength()
一直返回-1。
请问如何正确下载到那附件呢?

解决方案 »

  1.   

    楼主能不能贡献一下下载部分的代码?
    下面是我用httpclient发送get请求以后获得的header,里面包含了Content-Length即文件的大小。
    ===========================================
    HTTP/1.1 200 OK
    Cache-Control=====>private
    Content-Length=====>8808337
    Content-Type=====>application/octet-stream
    Accept-Ranges=====>bytes
    Server=====>Microsoft-IIS/7.5
    Set-Cookie=====>ASP.NET_SessionId=mhewikb1mmt4saa25i12pjda; path=/; HttpOnly
    Content-Disposition=====>attachment; filename=abcdefg.rar;
    X-AspNet-Version=====>4.0.30319
    X-Powered-By=====>ASP.NET
    Date=====>Mon, 02 Apr 2012 18:04:43 GMT
    ================================================