钛雷,最强装逼打脸系统,鹰阜大队
本文实例为大家分享了java实现客户端向服务器发送文件的具体代码,供大家参考,具体内容如下
服务器源代码:
import java.io.bufferedreader; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.serversocket; import java.net.socket; /** * * 文件名:serverreceive.java * 实现功能:作为服务器接收客户端发送的文件 * * 具体实现过程: * 1、建立socketserver,等待客户端的连接 * 2、当有客户端连接的时候,按照双方的约定,这时要读取一行数据 * 其中保存客户端要发送的文件名和文件大小信息 * 3、根据文件名在本地创建文件,并建立好流通信 * 4、循环接收数据包,将数据包写入文件 * 5、当接收数据的长度等于提前文件发过来的文件长度,即表示文件接收完毕,关闭文件 * 6、文件接收工作结束 * * * 【注:此代码仅为演示客户端与服务器传送文件使用, * 每一个数据包之前没有文件协议命令 * 具体的协议传输和文件传出的使用阶段可根据自己程序自行放置】 * * * 作者:小菜鸟 * 创建时间:2014-08-19 * * */ public class serverreceive { public static void main(string[] args) { /**与服务器建立连接的通信句柄*/ serversocket ss = null; socket s = null; /**定义用于在接收后在本地创建的文件对象和文件输出流对象*/ file file = null; fileoutputstream fos = null; /**定义输入流,使用socket的inputstream对数据包进行输入*/ inputstream is = null; /**定义byte数组来作为数据包的存储数据包*/ byte[] buffer = new byte[4096 * 5]; /**用来接收文件发送请求的字符串*/ string comm = null; /**建立socekt通信,等待服务器进行连接*/ try { ss = new serversocket(4004); s = ss.accept(); } catch (ioexception e) { e.printstacktrace(); } /**读取一行客户端发送过来的约定信息*/ try { inputstreamreader isr = new inputstreamreader(s.getinputstream()); bufferedreader br = new bufferedreader(isr); comm = br.readline(); } catch (ioexception e) { system.out.println("服务器与客户端断开连接"); } /**开始解析客户端发送过来的请求命令*/ int index = comm.indexof("/#"); /**判断协议是否为发送文件的协议*/ string xieyi = comm.substring(0, index); if(!xieyi.equals("111")){ system.out.println("服务器收到的协议码不正确"); return; } /**解析出文件的名字和大小*/ comm = comm.substring(index + 2); index = comm.indexof("/#"); string filename = comm.substring(0, index).trim(); string filesize = comm.substring(index + 2).trim(); /**创建空文件,用来进行接收文件*/ file = new file(filename); if(!file.exists()){ try { file.createnewfile(); } catch (ioexception e) { system.out.println("服务器端创建文件失败"); } }else{ /**在此也可以询问是否覆盖*/ system.out.println("本路径已存在相同文件,进行覆盖"); } /**【以上就是客户端代码中写到的服务器的准备部分】*/ /** * 服务器接收文件的关键代码*/ try { /**将文件包装到文件输出流对象中*/ fos = new fileoutputstream(file); long file_size = long.parselong(filesize); is = s.getinputstream(); /**size为每次接收数据包的长度*/ int size = 0; /**count用来记录已接收到文件的长度*/ long count = 0; /**使用while循环接收数据包*/ while(count < file_size){ /**从输入流中读取一个数据包*/ size = is.read(buffer); /**将刚刚读取的数据包写到本地文件中去*/ fos.write(buffer, 0, size); fos.flush(); /**将已接收到文件的长度+size*/ count += size; system.out.println("服务器端接收到数据包,大小为" + size); } } catch (filenotfoundexception e) { system.out.println("服务器写文件失败"); } catch (ioexception e) { system.out.println("服务器:客户端断开连接"); }finally{ /** * 将打开的文件关闭 * 如有需要,也可以在此关闭socket连接 * */ try { if(fos != null) fos.close(); } catch (ioexception e) { e.printstacktrace(); }//catch (ioexception e) }//finally }//public static void main(string[] args) }//public class serverreceive
客户端源代码:
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.outputstream; import java.io.printstream; import java.net.socket; /** * * 文件名:clientsend.java * 实现功能:作为客户端向服务器发送一个文件 * * 具体实现过程: * 1、建立与服务器端的连接,ip:127.0.0.1, port:4004 * 2、将文件的名字和大小通过自定义的文件传输协议,发送到服务器 * 3、循环读取本地文件,将文件打包发送到数据输出流中 * 4、关闭文件,结束传输 * * 【注:此代码仅为演示客户端与服务器传送文件使用, * 每一个数据包之前没有文件协议命令 * 具体的协议传输和文件传出的使用阶段可根据自己程序自行放置】 * * * 作者:小菜鸟 * 创建时间:2014-08-19 * * */ public class clientsend { public static void main(string[] args) { /**与服务器建立连接的通信句柄*/ socket s = null; /**定义文件对象,即为要发送的文件 * 如果使用绝对路径,不要忘记使用'/'和'\'的区别 * 具体区别,请读者自行查询 * */ file sendfile = new file("api.chm"); /**定义文件输入流,用来打开、读取即将要发送的文件*/ fileinputstream fis = null; /**定义byte数组来作为数据包的存储数据包*/ byte[] buffer = new byte[4096 * 5]; /**定义输出流,使用socket的outputstream对数据包进行输出*/ outputstream os = null; /**检查要发送的文件是否存在*/ if(!sendfile.exists()){ system.out.println("客户端:要发送的文件不存在"); return; } /**与服务器建立连接*/ try { s = new socket("127.0.0.1", 4004); }catch (ioexception e) { system.out.println("未连接到服务器"); } /**用文件对象初始化fis对象 * 以便于可以提取出文件的大小 * */ try { fis = new fileinputstream(sendfile); } catch (filenotfoundexception e1) { e1.printstacktrace(); } /**首先先向服务器发送关于文件的信息,以便于服务器进行接收的相关准备工作 * 具体的准备工作,请查看服务器代码。 * * 发送的内容包括:发送文件协议码(此处为111)/#文件名(带后缀名)/#文件大小 * */ try { printstream ps = new printstream(s.getoutputstream()); ps.println("111/#" + sendfile.getname() + "/#" + fis.available()); ps.flush(); } catch (ioexception e) { system.out.println("服务器连接中断"); } /** * 此处睡眠2s,等待服务器把相关的工作准备好 * 也是为了保证网络的延迟 * 读者可自行选择添加此代码 * */ try { thread.sleep(2000); } catch (interruptedexception e1) { e1.printstacktrace(); } /**之前的准备工作结束之后 * 下面就是文件传输的关键代码 * */ try { /**获取socket的outputstream,以便向其中写入数据包*/ os = s.getoutputstream(); /** size 用来记录每次读取文件的大小*/ int size = 0; /**使用while循环读取文件,直到文件读取结束*/ while((size = fis.read(buffer)) != -1){ system.out.println("客户端发送数据包,大小为" + size); /**向输出流中写入刚刚读到的数据包*/ os.write(buffer, 0, size); /**刷新一下*/ os.flush(); } } catch (filenotfoundexception e) { system.out.println("客户端读取文件出错"); } catch (ioexception e) { system.out.println("客户端输出文件出错"); }finally{ /** * 将打开的文件关闭 * 如有需要,也可以在此关闭socket连接 * */ try { if(fis != null) fis.close(); } catch (ioexception e) { system.out.println("客户端文件关闭出错"); }//catch (ioexception e) }//finally }//public static void main(string[] args) }//public class clientsend
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
apollo与springboot集成实现动态刷新配置的教程详解
网友评论