ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:20.18KB ,
资源ID:3086816      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-3086816.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java多线程网络传输文件非同步.docx)为本站会员(b****2)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

java多线程网络传输文件非同步.docx

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