2个回答
展开全部
点对点传输文件
/*
import java.io.*;
import java.net.*;
import java.util.*;
*/
private HttpURLConnection connection;//存储连接
private int downsize = -1;//下载文件大小,初始值为-1
private int downed = 0;//文加已下载大小,初始值为0
private RandomAccessFile savefile;//记录下载信息存储文件
private URL fileurl;//记录要下载文件的地址
private DataInputStream fileStream;//记录下载的数据流
try{
/*开始创建下载的存储文件,并初始化值*/
File tempfileobject = new File("h:\\webwork-2.1.7.zip");
if(!tempfileobject.exists()){
/*文件不存在则建立*/
tempfileobject.createNewFile();
}
savefile = new RandomAccessFile(tempfileobject,"rw");
/*建立连接*/
fileurl = new URL("https://webwork.dev.java.net/files/documents/693/9723/webwork-2.1.7.zip");
connection = (HttpURLConnection)fileurl.openConnection();
connection.setRequestProperty("Range","byte="+this.downed+"-");
this.downsize = connection.getContentLength();
//System.out.println(connection.getContentLength());
new Thread(this).start();
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("构建器错误");
System.exit(0);
}
public void run(){
/*开始下载文件,以下测试非断点续传,下载的文件存在问题*/
try{
System.out.println("begin!");
Date begintime = new Date();
begintime.setTime(new Date().getTime());
byte[] filebyte;
int onecelen;
//System.out.println(this.connection.getInputStream().getClass().getName());
this.fileStream = new DataInputStream(
new BufferedInputStream(
this.connection.getInputStream()));
System.out.println("size = " + this.downsize);
while(this.downsize != this.downed){
if(this.downsize - this.downed > 262144){//设置为最大256KB的缓存
filebyte = new byte[262144];
onecelen = 262144;
}
else{
filebyte = new byte[this.downsize - this.downed];
onecelen = this.downsize - this.downed;
}
onecelen = this.fileStream.read(filebyte,0,onecelen);
this.savefile.write(filebyte,0,onecelen);
this.downed += onecelen;
System.out.println(this.downed);
}
this.savefile.close();
System.out.println("end!");
System.out.println(begintime.getTime());
System.out.println(new Date().getTime());
System.out.println(begintime.getTime() - new Date().getTime());
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("run()方法有问题!");
}
}
/***
//FileClient.java
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) throws Exception {
//使用本地文件系统接受网络数据并存为新文件
File file = new File("d:\\fmd.doc");
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 通过Socket连接文件服务器
Socket server = new Socket(InetAddress.getLocalHost(), 3318);
//创建网络接受流接受服务器文件数据
InputStream netIn = server.getInputStream();
InputStream in = new DataInputStream(new BufferedInputStream(netIn));
//创建缓冲区缓冲网络数据
byte[] buf = new byte[2048];
int num = in.read(buf);
while (num != (-1)) {//是否读完所有数据
raf.write(buf, 0, num);//将数据写往文件
raf.skipBytes(num);//顺序写文件字节
num = in.read(buf);//继续从网络中读取文件
}
in.close();
raf.close();
}
}
//FileServer.java
import java.io.*;
import java.util.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws Exception {
//创建文件流用来读取文件中的数据
File file = new File("d:\\系统特点.doc");
FileInputStream fos = new FileInputStream(file);
//创建网络服务器接受客户请求
ServerSocket ss = new ServerSocket(8801);
Socket client = ss.accept();
//创建网络输出流并提供数据包装器
OutputStream netOut = client.getOutputStream();
OutputStream doc = new DataOutputStream(
new BufferedOutputStream(netOut));
//创建文件读取缓冲区
byte[] buf = new byte[2048];
int num = fos.read(buf);
while (num != (-1)) {//是否读完文件
doc.write(buf, 0, num);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
num = fos.read(buf);//继续从文件中读取数据
}
fos.close();
doc.close();
}
}
*/
/*
import java.io.*;
import java.net.*;
import java.util.*;
*/
private HttpURLConnection connection;//存储连接
private int downsize = -1;//下载文件大小,初始值为-1
private int downed = 0;//文加已下载大小,初始值为0
private RandomAccessFile savefile;//记录下载信息存储文件
private URL fileurl;//记录要下载文件的地址
private DataInputStream fileStream;//记录下载的数据流
try{
/*开始创建下载的存储文件,并初始化值*/
File tempfileobject = new File("h:\\webwork-2.1.7.zip");
if(!tempfileobject.exists()){
/*文件不存在则建立*/
tempfileobject.createNewFile();
}
savefile = new RandomAccessFile(tempfileobject,"rw");
/*建立连接*/
fileurl = new URL("https://webwork.dev.java.net/files/documents/693/9723/webwork-2.1.7.zip");
connection = (HttpURLConnection)fileurl.openConnection();
connection.setRequestProperty("Range","byte="+this.downed+"-");
this.downsize = connection.getContentLength();
//System.out.println(connection.getContentLength());
new Thread(this).start();
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("构建器错误");
System.exit(0);
}
public void run(){
/*开始下载文件,以下测试非断点续传,下载的文件存在问题*/
try{
System.out.println("begin!");
Date begintime = new Date();
begintime.setTime(new Date().getTime());
byte[] filebyte;
int onecelen;
//System.out.println(this.connection.getInputStream().getClass().getName());
this.fileStream = new DataInputStream(
new BufferedInputStream(
this.connection.getInputStream()));
System.out.println("size = " + this.downsize);
while(this.downsize != this.downed){
if(this.downsize - this.downed > 262144){//设置为最大256KB的缓存
filebyte = new byte[262144];
onecelen = 262144;
}
else{
filebyte = new byte[this.downsize - this.downed];
onecelen = this.downsize - this.downed;
}
onecelen = this.fileStream.read(filebyte,0,onecelen);
this.savefile.write(filebyte,0,onecelen);
this.downed += onecelen;
System.out.println(this.downed);
}
this.savefile.close();
System.out.println("end!");
System.out.println(begintime.getTime());
System.out.println(new Date().getTime());
System.out.println(begintime.getTime() - new Date().getTime());
}
catch(Exception e){
System.out.println(e.toString());
System.out.println("run()方法有问题!");
}
}
/***
//FileClient.java
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) throws Exception {
//使用本地文件系统接受网络数据并存为新文件
File file = new File("d:\\fmd.doc");
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 通过Socket连接文件服务器
Socket server = new Socket(InetAddress.getLocalHost(), 3318);
//创建网络接受流接受服务器文件数据
InputStream netIn = server.getInputStream();
InputStream in = new DataInputStream(new BufferedInputStream(netIn));
//创建缓冲区缓冲网络数据
byte[] buf = new byte[2048];
int num = in.read(buf);
while (num != (-1)) {//是否读完所有数据
raf.write(buf, 0, num);//将数据写往文件
raf.skipBytes(num);//顺序写文件字节
num = in.read(buf);//继续从网络中读取文件
}
in.close();
raf.close();
}
}
//FileServer.java
import java.io.*;
import java.util.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws Exception {
//创建文件流用来读取文件中的数据
File file = new File("d:\\系统特点.doc");
FileInputStream fos = new FileInputStream(file);
//创建网络服务器接受客户请求
ServerSocket ss = new ServerSocket(8801);
Socket client = ss.accept();
//创建网络输出流并提供数据包装器
OutputStream netOut = client.getOutputStream();
OutputStream doc = new DataOutputStream(
new BufferedOutputStream(netOut));
//创建文件读取缓冲区
byte[] buf = new byte[2048];
int num = fos.read(buf);
while (num != (-1)) {//是否读完文件
doc.write(buf, 0, num);//把文件数据写出网络缓冲区
doc.flush();//刷新缓冲区把数据写往客户端
num = fos.read(buf);//继续从文件中读取数据
}
fos.close();
doc.close();
}
}
*/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询