1、java多线程网络传输文件非同步用socket发送xml文档,把生成的xml文档转化成字符流输出Socket c = new Socket(host,port) PrintWriter pw = new PrintWriter(new OutuputStreamWriter(c.getOutputStream(),true); File f = new File(xml); BufferedReader br = new BufferedReader(new FileReader(f); String line; while(line = br.readLine() != null) pw.p
2、rintln(line); pw.close(); br.close();java多线程网络传输文件(非同步)利用 Socket 套接字进行面向连接通信的编程。客户端读取本地文件并发送;服务器接收文件并保存到本地文件系统中。 使用说明:请将TransferClient, TransferServer, TempFile三个类编译,他们的类包是FileServer.客户端:修改TransferClient: serPort, serIP, filePath, blockNum,的值来符合您机器的系统环境;或者用命令行格式: java FileServer.TransferClient 文件路径+
3、文件名 服务器IP地址 端口 线程数C:java FileServer.TransferClient e:GREbulletin.pdf 192.168.1.105 2008 5服务端:修改TransferServer: serPort, tempFolder的值来符合您机器的系统环境;或者用命令行格式: java FileServer.TransferServer 端口 文件夹路径C:java FileServer.TransferServer 2008 F:tempFolder文件传输服务端-package FileServer;import java.io.DataInputStream
4、;import java.io.DataOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.RandomAccessFile;import .ServerSocket;import .Socket;import .SocketException;/* Description Socket文件传输服务端* 默认支持多线程传输,未支持断点续传,未支持内网UDF传输* Creation Date
5、 07-08-2008 9:00* author * version 1.0*/public class TransferServer /* * param args0 端口默认2008(例: 2008) * param args1 接收文件存放路径,默认:和FileServer同一目录ServerReceive * (例:x:exampleexample) */public static void main(String args) try int serPort = 2008;/服务器默认端口2008String tempFolder = System.getProperty(user.d
6、ir)+ServerReceive;/临时文件夹,用于存放接收文件ServerSocket ss = null;/设定Socketif (args.length0) tempFolder = args1;System.out.println(Welcome to TransferServer);System.out.println(服务器启动,监听端口 + ss.getLocalPort();System.out.println(服务器临时文件路径: +tempFolder);while (true) Socket s = ss.accept();new Thread(new ServerTh
7、read(s,tempFolder).start(); catch (IOException e) e.getMessage();System.out.println(服务器端口被占用,或其他问题.);/* Description 服务器线程* author 卢俊宇* param tempFolder 接收文件存放路径,默认:TransferServer.class所在目录ServerReceive* (例:x:exampleexample)* version 1.1*/class ServerThread implements Runnable Socket s;/ 实例化socket类pr
8、ivate String tempFolder;/临时文件夹名public ServerThread(Socket s,String tempFolder) this.s = s;this.tempFolder = tempFolder;SuppressWarnings(deprecation)public void run() try InputStream ins = s.getInputStream();OutputStream outs = s.getOutputStream();DataInputStream dis = new DataInputStream(ins);DataOu
9、tputStream dos = new DataOutputStream(outs);/ 取得线程IDString SerID = SerID- + Thread.currentThread().getId();while (true) try String inStr = dis.readUTF();/ 接收到的文件包含头信息if (inStr != null) / 对收到的socket包过滤出文件信息if (inStr.contains(ClientInfo) System.out.println(SerID + get FileInfo! );/ 文件名字String fName =
10、new String(inStr.replaceAll(.+)|(S+), ).toString().getBytes(utf-8), utf-8);/ 文件总长度String fSize = new String(inStr.replaceAll(.+)|, ).toString().getBytes(utf-8), utf-8);System.out.println(size: + fSize);long Size = Long.parseLong(fSize);/ 区块起始长度String fPs = new String(inStr.replaceAll(.+)|(S+),).toSt
11、ring().getBytes(utf-8), utf-8);System.out.println(PS: + fPs);/ 区块结束长度String fPe = new String(inStr.replaceAll(.+)|(S+),).toString().getBytes(utf-8), utf-8);System.out.println(PE: + fPe);long PointS = Long.parseLong(fPs); / 分块头long PointE = Long.parseLong(fPe); / 分块尾System.out.println(SourceFile Name
12、 : + fName);File tempF = new File(tempFolder, fName);if (!tempF.exists() /检测目标文件夹是否存在if (new File(tempFolder).isDirectory() TempFile.creat(tempF, Size);/ 如果临时文件不存在就建立else boolean creat = new File(tempFolder).mkdirs();if (creat) TempFile.creat(tempF, Size);/ 如果临时文件不存在就建立else System.out.println(Error:
13、System can not creat folder!);System.out.println(SerID+ exits);dis.close();/ 关闭包类dos.close();/ 关闭输出流s.close();/ 关连接Thread.currentThread().stop();/ 返回服务器准备好了String SerReady = SerID + ServerReady=1;dos.writeUTF(SerReady);/ 返回服务器信息dos.flush();/ 取得客户端发送标志SendStartif (dis.readUTF().equals(SendStart) / 随机
14、读写文件(适应多线程方式)RandomAccessFile fos = new RandomAccessFile(tempF.getPath(), rw);long curPoint = PointS;/ 文件指针起点long endSet = PointE;/ 文件指针终点long nowSize = 0;/ 已读/写字节byte buffer = new byte1024; / 建立缓冲区while (curPoint endSet) int length = 0;try if (endSet - curPoint + 1) 1024) fos.seek(curPoint);/ 寻找写入位
15、置byte bufferM = new byte(int) (endSet- curPoint + 1);/ 调整缓冲区大小length = ins.read(bufferM);/ 读取bufferM缓冲socket流try fos.write(bufferM);/ 将取得的socket流写入文件dos.writeUTF(SerGotIt);/ 返回服务器信息dos.flush();curPoint += length;/ 文件指针增加nowSize += length;fos.close();/ 向发送接收结束标志dos.writeUTF(ReciveEnd);dos.flush();Sys
16、tem.out.println(SerID+ is receive ok.);break; catch (IOException e) e.printStackTrace(); else fos.seek(curPoint);length = ins.read(buffer); catch (IOException e) e.printStackTrace();System.out.println(SerID + is abnormally closed.);break;if (length = buffer.length) / 将缓冲区字节写入文件try System.out.println
17、(SerID+ receive + buffer.length+ bytes.);fos.write(buffer);dos.writeUTF(SerGotIt);/ 返回服务器信息dos.flush();curPoint += buffer.length;/ 指针+1024nowSize += buffer.length;System.out.println(SerID + 指针位置 + curPoint);System.out.println(SerID+ 线程size写入进度: + nowSize); catch (IOException e) e.printStackTrace();/
18、 接收到客户端结束标志,跳出循环if (dis.readUTF().equals(SendEnd) System.out.println(服务器接收完毕);break; catch (SocketException s) / s.printStackTrace();/ 客户端非正常退出处理System.out.println(SerID + is abnormally closed.);break;dis.close();/ 关闭包类dos.close();/ 关闭输出流s.close();/ 关连接System.out.println(SerID + exited.); catch (IOE
19、xception e) e.printStackTrace();文件传输客户端-package FileServer;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.RandomAccessFile;import .Socket;import .InetAddress;import .U
20、nknownHostException;import java.text.SimpleDateFormat;import java.util.Date;/* Description Socket文件传输客户端(默认支持多线程传输,未支持断点续传,未支持内网UDF传输) * Creation Date 07-08-2008 9:00* author 卢俊宇* version 1.0*/public class TransferClient /* * Description 文件传输客户端 * * param args0 * 文件路径+文件名(例:x:exampleexample.class) *
21、 param args1 * 服务器IP地址(例: 192.168.1.1) * param args2 * 端口默认2008(例: 2008) * param args3 * 区块长度默认5 (例: 5) */public static void main(String args) int serPort = 2008;/ 默认连接端口2008;int blockNum = 5;/ 文件区块数量(同时决定线程数)String serIP = 192.168.1.105;/ 服务器IPString filePath = e:1.rar;/ 欲发送文件路径+文件名if (args.length0
22、) filePath = args0;if (args1.length()0) serIP = args1;if (args2.length()0) serPort = Integer.parseInt(args2);if (args3.length()0) blockNum = Integer.parseInt(args3.replaceAll(D, );System.out.println(文件路径: +filePath);System.out.println(连接IP: +serIP);System.out.println(连接端口: +serPort);System.out.println(线程数: +blockNum);/ 取得文件信息File source = new File(filePath);if (!source.exists() System.out.println(要传输的文件不存在!);System.exit(0);long fSize = source.length(
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2