package com.net.demo;
//Download by HTTP
//For other protocol, do it yourself
import java.net.*;
import java.io.*;
public class HttpDownLoad {
public static void main(String args[]) {
HttpDownLoad dl = new HttpDownLoad();
String downfile = "http://www.chinagps.cc/download/StormII_244.exe"; // can be any file type! Not only htm files can be downloaded.
String saveplace = "d:\\temp\\StormII_244.exe";
if (dl.downLoadFile(downfile, saveplace))
System.out.println("下载完毕!");
if (dl.downLoadServerFile(downfile, "d:\\temp\\StormII_244_1.exe"))
System.out.println("下载完毕!");
}
public boolean downLoadFile(String fileurl, String savepath) {
try {
URL url = new URL(fileurl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
int filelen = conn.getContentLength();
byte[] filebuf = new byte[filelen];
byte[] tmpbuf = new byte[1024];
int readcount = 0;
int readnum = 0;
while (readcount < filelen && readnum != -1) {
readnum = is.read(tmpbuf);
if (readnum > -1) {
System.arraycopy(tmpbuf, 0, filebuf, readcount, readnum);
readcount = readcount + readnum;
}
}
if (readcount < filelen) {
System.out.println("下载文件错误!");
return false;
}
File savefile = new File(savepath);
if (!savefile.exists())
savefile.createNewFile();
FileOutputStream fos = new FileOutputStream(savefile);
fos.write(filebuf);
fos.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean downLoadServerFile(String fileurl, String savepath) {//Things need to do:
//Set connection timeout
//Try more times to download
//Catch a general Exception is not so good
//...
try {
int httpStatusCode;
URL url = new URL(fileurl);
URLConnection conn = url.openConnection();
conn.connect();
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpStatusCode = httpconn.getResponseCode();
if (httpStatusCode != HttpURLConnection.HTTP_OK) {//HttpURLConnection return an error code
System.out.println("连接 " + fileurl+ " 错误,返回的代码是:" + httpStatusCode);
return false;
}
int filelen = conn.getContentLength();
InputStream is = conn.getInputStream();
byte[] tmpbuf = new byte[1024];
File savefile = new File(savepath);
if (!savefile.exists())
savefile.createNewFile();
FileOutputStream fos = new FileOutputStream(savefile);
int readnum = 0;
if (filelen < 0)//for http://www.csdn.net/expert/topic/204/204361.shtm, conn.getContentLength() return -1.
{
while (readnum > -1) {
readnum = is.read(tmpbuf);
if (readnum > 0)
fos.write(tmpbuf, 0, readnum);
}
} else {
int readcount = 0;
while (readcount < filelen && readnum != -1) {
readnum = is.read(tmpbuf);
if (readnum > -1) {
fos.write(tmpbuf, 0, readcount);
readcount = readcount + readnum;
}
}
if (readcount < filelen) {
System.out.println("下载错误");
is.close();
fos.close();
savefile.delete();
return false;
}
}
fos.flush();
fos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个通过Socket或URLConnection传文件的示例
共40个文件
class:18个
java:16个
jsp:1个
5星 · 超过95%的资源 需积分: 12 203 下载量 92 浏览量
2008-09-04
23:11:10
上传
评论
收藏 38KB RAR 举报
温馨提示
一个通过Socket或URLConnection传文件的示例 Socket URLConnection 文件传输
资源推荐
资源详情
资源评论
收起资源包目录
netfiledemo.rar (40个子文件)
src
com
net
demo
FileServer.java 962B
PooledRemoteFileServer.java 1KB
ConnectionHandler.java 942B
PooledConnectionHandler.java 2KB
HttpDownLoad.java 3KB
myserver.java 2KB
myclient.java 2KB
FileClient.java 985B
MultiThreadClient.java 1KB
SocketFileClient.java 3KB
MultithreadedRemoteFileServer.java 1KB
SocketFileServer.java 2KB
ClientSocket.java 2KB
MultiThreadServer.java 2KB
RemoteFileClient.java 2KB
RemoteFileServer.java 2KB
.classpath 340B
.mymetadata 306B
.myeclipse
.project 1013B
WebRoot
META-INF
MANIFEST.MF 39B
index.jsp 834B
WEB-INF
classes
com
net
demo
PooledConnectionHandler.class 2KB
MultiThreadClient.class 975B
SocketFileServer.class 2KB
Handler.class 3KB
HttpDownLoad.class 3KB
myserver.class 2KB
PooledRemoteFileServer.class 2KB
MultiThreadClient$1.class 2KB
MultiThreadServer.class 2KB
FileClient.class 2KB
RemoteFileServer.class 3KB
MultithreadedRemoteFileServer.class 2KB
ConnectionHandler.class 2KB
SocketFileClient.class 3KB
myclient.class 2KB
ClientSocket.class 2KB
FileServer.class 1KB
RemoteFileClient.class 3KB
lib
web.xml 375B
共 40 条
- 1
冷月宫主
- 粉丝: 1185
- 资源: 364
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页