Java实现基于http协议的网络文件下载
2015-12-12 23:59
761 查看
try {
URL url = new URL(sURL);
//打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
//获得文件长度
long nEndPos =getFileSize(sURL);
RandomAccessFile oSavedFile= new RandomAccessFile(sPath+"//"+sName, "rw");
httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
//告诉服务器book.rar这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
//读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0
&& nStartPos < nEndPos ) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}
}
URL url = new URL(sURL);
//打开连接
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
//获得文件长度
long nEndPos =getFileSize(sURL);
RandomAccessFile oSavedFile= new RandomAccessFile(sPath+"//"+sName, "rw");
httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
String sProperty = "bytes=" + nStartPos + "-";
//告诉服务器book.rar这个文件从nStartPos字节开始传
httpConnection.setRequestProperty("RANGE", sProperty);
System.out.println(sProperty);
InputStream input = httpConnection.getInputStream();
byte[] b = new byte[1024];
//读取网络文件,写入指定的文件中
while ((nRead = input.read(b, 0, 1024)) > 0
&& nStartPos < nEndPos ) {
oSavedFile.write(b, 0, nRead);
nStartPos += nRead;
}
httpConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
// 获得文件长度
public static long getFileSize(String sURL) {
int nFileLength = -1;
try {
URL url = new URL(sURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
httpConnection.setRequestProperty("User-Agent", "Internet Explorer");
int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
System.err.println("Error Code : " + responseCode);
return -2; // -2 represent access is error
}
String sHeader;
for (int i = 1;; i++) {
sHeader = httpConnection.getHeaderFieldKey(i);
if (sHeader != null) {
if (sHeader.equals("Content-Length")) {
nFileLength = Integer.parseInt(httpConnection
.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(nFileLength);
return nFileLength;
}
}
相关文章推荐
- codeforces 510E. Fox And Dinner 网络流
- DetNet 确定性的网络
- HttpHandler,HttpApplication, HttpModule
- 牛客网 debug-release http://www.nowcoder.com/profile/380772/test/2198844/36245?onlyWrong=0
- 牛客网 鼠标事件 http://www.nowcoder.com/profile/380772/test/2198844/16026?onlyWrong=0
- 牛客网 http://www.nowcoder.com/profile/380772/test/2198844/25222?onlyWrong=0
- Final Http的简要介绍与使用
- 使用HttpURLConnection实现图片的下载与现显示
- Linux网络配置与管理
- 判断网络连接状态并自动界面跳转
- 《RocketMq》一、网络传输篇
- 大陆为Android Studio设置HTTP代理
- android studio中在api23版本中使用HttpClient的问题
- HTTP协议
- 检查是否有网络工具类
- 简单的感知学习原理例子 人工神经网络 Perceptron Learning Rule
- 关于Http协议(2)--转载
- android5.0无法使用HttpClient解决办法
- c++父类指针指向子类的具体原理? http://www.zhihu.com/question/31345300
- 网络基础知识HTTP(1) --转载