本帖最后由 经济 于 2015-6-5 00:39 编辑
本人自己写的多线程上传,望大家给予点评
客户端源码:
package com.dyn.itheima.test8;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
// 创建字节输出流对象
BufferedInputStream inputStream = null;
// 创建字节输出流对象
BufferedOutputStream outputStream = null;
// 创建套接字对象
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 10086);
// 封装输出流对象
inputStream = new BufferedInputStream(new FileInputStream(
"C:\\Users\\Administrator\\Desktop\\aa.jpg"));
// 封装输出流对象
outputStream = new BufferedOutputStream(socket.getOutputStream());
// 数据写入服务器
byte[] data = new byte[1024];
int len = 0;
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
outputStream.flush();
}
socket.shutdownOutput();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(socket.isClosed()){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
服务器源码:
package com.dyn.itheima.test8;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
//创建服务端套接字
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(10086);
//开启服务线程
while(true){
Socket socket = serverSocket.accept();
new Thread(new ServerThread(socket)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(serverSocket.isClosed()){
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
服务器线程源码:
package com.dyn.itheima.test8;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerThread implements Runnable {
// 创建套接字对象
private Socket socket = null;
// 创建字节输出流对象
private BufferedInputStream inputStream = null;
// 创建字节输出流对象
private BufferedOutputStream outputStream = null;
public ServerThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
// 获取套接字输入流,并将其封装到BufferedInputStream中
inputStream = new BufferedInputStream(socket.getInputStream());
// 封装outputStream对象
outputStream = new BufferedOutputStream(new FileOutputStream(
"dyn.jpg"));
// 声明数组缓冲区
byte[] data = new byte[1024];
int len = 0;
// 将数据输出
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
// 刷新缓冲区
outputStream.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
|
|