多线程和网络通信实现的简单多线程上传
来源:互联网 发布:kali linux dsdt 编辑:程序博客网 时间:2024/04/29 03:55
package com.qianfeng.tcpupload;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;public class UploadPicClient {public static void main(String[] args) throws UnknownHostException, IOException {// TODO Auto-generated method stub// 1,创建客户端socket。Socket s = new Socket("127.0.0.1", 10006);// 2,读取客户端要上传的图片文件。FileInputStream fis = new FileInputStream("c:\\Sunset.jpg");// 3,获取socket输出流,将读到图片数据发送给服务端。OutputStream out = s.getOutputStream();byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {out.write(buf, 0, len);}// 告诉服务端说:这边的数据发送完毕。让服务端停止读取。s.shutdownOutput();// 读取服务端发回的内容。InputStream in = s.getInputStream();byte[] bufIn = new byte[1024];int lenIn = in.read(buf);String text = new String(buf, 0, lenIn);System.out.println(text);fis.close();s.close();}}
package com.qianfeng.tcpupload;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class UploadPicServer {public static void main(String[] args) throws IOException {// TODO Auto-generated method stub//创建tcp的socket服务端。ServerSocket ss = new ServerSocket(10006);while(true){Socket s = ss.accept();new Thread(new UploadTask(s)).start();}}}
package com.qianfeng.tcpupload;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class UploadTask implements Runnable {private static final int SIZE = 1024 * 1024 * 2;private Socket s;public UploadTask(Socket s) {this.s = s;}@Overridepublic void run() {int count = 0;String ip = s.getInetAddress().getHostAddress();System.out.println(ip + ".....connected");try {// 读取客户端发来的数据。InputStream in = s.getInputStream();// 将读取到数据存储到一个文件中。File dir = new File("c:\\pic");if (!dir.exists()) {dir.mkdirs();}File file = new File(dir, ip + ".jpg");// 如果文件已经存在于服务端while (file.exists()) {file = new File(dir, ip + "(" + (++count) + ").jpg");}FileOutputStream fos = new FileOutputStream(file);byte[] buf = new byte[1024];int len = 0;while ((len = in.read(buf)) != -1) {fos.write(buf, 0, len);if (file.length() > SIZE) {System.out.println(ip + "文件体积过大");fos.close();s.close();System.out.println(ip + "...." + file.delete());return;}}// 获取socket输出流,将上传成功字样发给客户端。OutputStream out = s.getOutputStream();out.write("上传成功".getBytes());fos.close();s.close();} catch (IOException e) {}}}
0 0
- 多线程和网络通信实现的简单多线程上传
- 多线程和网络通信实现的简单文件下载
- 实现了UI的简单群聊(java的IO流和多线程,网络通信)
- 简单多线程和网络通信模拟聊天
- 多线程的网络通信
- Android 网络:基于TCP协议通信,多线程,实现简单的C/S聊天室
- 简单的多线程和网络通信的模拟(求三角形面积)
- 多线程通信的简单实践
- 网络编程 UDP通信的过程 TCP通信过程 多线程文件上传
- 简单实现基于TCP的Socket多线程通信
- 简单实现基于UDP的Socket多线程通信
- Socket加入多线程通信实现简单的CS聊天室
- 网络多线程-NSThread实现线程间通信
- 黑马程序员---多线程和网络通信
- [网络和多线程]5、线程通信
- 多线程网络通信
- 多线程版本网络通信
- 编写多线程网络检测程序的简单实现
- c++类的内存布局
- 关于DBCP断网数据库连接失效的解决方案
- a simple lisp Interpreter
- Android4.2.2的preview的数据流和控制流以及最终的预览显示
- 多线程的嵌套锁
- 多线程和网络通信实现的简单多线程上传
- 快速排序
- Java Socket编程
- IPC通信比较
- 用java实现上传文件
- 多文件修改版
- android调试是出现:Re-installation failed due to different application signatures
- TrustZone——例子程序—FastModels和QEMU测试
- 生病不爽呢