[十一]java作业
来源:互联网 发布:网络剧怎么赚钱 编辑:程序博客网 时间:2024/04/30 02:37
多线程文件传送
//serverimport java.io.*;import java.net.*;public class servertest extends ServerSocket{class ServerThread extends Thread {private Socket client ;private File files = null;private FileInputStream inputtmp = null ; private DataOutputStream netout = null ;public ServerThread(Socket tmp)throws IOException {client = tmp ;files = new File("server.jpg");inputtmp = new FileInputStream(files);netout = new DataOutputStream(client.getOutputStream());System.out.println("Client("+client.getInetAddress().getHostAddress()+") connect");start();}public void run(){try {netout.writeUTF(files.getName());netout.writeLong(files.length());byte[] sendBytes = new byte[1024];int length = 0 ;while((length = inputtmp.read(sendBytes, 0, sendBytes.length))>0){netout.write(sendBytes, 0, length);netout.flush();}client.close();netout.close();inputtmp.close();}catch(IOException e){e.printStackTrace();}}}public servertest() throws IOException{super(8580);System.out.println("server start:");for(;;){Socket socket = accept();new ServerThread(socket);}} @SuppressWarnings("resource")public static void main(String[]args){ try {new servertest();} catch (IOException e) {e.printStackTrace();} }}
//clientimport java.io.*;import java.net.*;public class clienttest {public static void main(String[]args){Socket client=null ;FileOutputStream filesoutput=null ;DataInputStream netin=null ;try {client=new Socket("127.0.0.1",8580);netin=new DataInputStream(client.getInputStream());String filesname=netin.readUTF();long length=netin.readLong();filesoutput=new FileOutputStream(new File("rec.jpg"));byte[]sendBytes=new byte[1024];System.out.println("rec files<"+filesname+"> <"+length+">");for(;;){int read=0 ;read=netin.read(sendBytes);if(read==-1)break ;filesoutput.write(sendBytes,0,read);}filesoutput.close();netin.close();client.close();System.out.println("<"+filesname+"> end");}catch(Exception e){e.printStackTrace();}}}
参考资料
https://my.oschina.net/leejun2005/blog/104955
0 0
- [十一]java作业
- 十一作业
- 作业 十一
- 十一章 作业
- 十一章 作业
- 十一章 作业
- [C语言学习]作业十一
- 十一章 作业 二题
- 十一章课后作业6
- linux十一十二单元作业
- 十一Java作业 按要求处理数据(代码待补充。。。)
- 十一月六日作业(双十一广告)
- JAVA作业
- Java作业
- Java作业
- java作业
- java作业
- JAVA作业
- 常用逻辑芯片
- spring mvc 的Controller类是单例(singleton)的吗?
- 拖欠的Java笔记
- OpenCV 3 - 编译更强大的OpenCV(三) - BUG与崩溃齐飞
- C++实现操作系统进程调度(时间片轮转法)
- [十一]java作业
- 51Nod 1278 相离的圆
- poj 3259 Wormholes
- python file
- Spring MVC 静态概念
- (笔记)Spring MVC学习指南_数据绑定和表单标签库
- python 正则表达式
- Windows内核中的内存管理
- Why are arrays of references illegal?