import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;public class DownloadsTest { public static void main(String[] args) {
// TODO Auto-generated method stub
try {
URL url = new URL("http://www.cnblogs.com/guoshiandroid/archive/2010/06/05/1752150.html");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
// http.setRequestProperty(key, value)
http.setRequestProperty("User-Agent","NetFox");
http.setRequestProperty("Connection", "Keep-Alive");
http.setRequestProperty("RANGE", "bytes=1024");
if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = http.getInputStream();
System.out.println(http.getContentLength());
new DownloadsTest().downloadFile(is, "chbb", "hello.html");
System.out.println("ok");
} else {
System.out.println("fail");
}
} catch (Exception e) {
e.printStackTrace();
}
//new DownloadsTest().downloadFile( "chbb", "aaa.txt");
} public void downloadFile(InputStream is,String dir,String filename) throws IOException {
File filedir = new File("D://"+dir);
filedir.mkdir();
File f = new File(filedir.getAbsolutePath() +"/"+ filename);
//f.createNewFile();
//System.out.println(filedir.getAbsolutePath());
DataInputStream in = new DataInputStream(is);
FileOutputStream out = new FileOutputStream(f);
byte [] b =new byte[1024*5];
while((in.read(b))!=-1) {
out.write(b);

}
out.flush();
}}在代码 中添加了http.setRequestProperty("RANGE", "bytes=1024");
后就连接不上服务器了,如果注释掉的话能够正常的下载了咯!
我做着个练习是为了能够实现断点续传的功能,可是这样的设置就连服务器都连不上了!郁闷了!下面什么都搞不了!
不懂是什么原因!  我用的tomcat服务器,是 要在配置文件里面设置什么吗?还是要设置其他的一些头信息啊!请各位大侠帮帮忙啊!