服务端:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class FileService implements Runnable{
public static void main(String[] args) {
try {
// 创建一个服务器端的ServerSocket,绑定并监听9999端口
ServerSocket serverSocket = new ServerSocket(9999);
int count = 0;// 记录客户端的数量
System.out.println("服务器启动,等待客户端的连接。。。");
Socket socket = null;
while (true) {
// 循环监听等待客户端的连接
socket = serverSocket.accept();
// 每当一个客户端连接到服务器,服务器启动一个单独的线程处理与此客户端的通信
++count;
Thread serverHandleThread = new Thread(new FileService(socket));
serverHandleThread.setPriority(4);// 设置线程的优先级[1,10],1为最低,默认是5
serverHandleThread.start();
System.out.println("上线的客户端有" + count + "个!");
InetAddress inetAddress = socket.getInetAddress();
System.out.println("当前客户端的IP地址是:" + inetAddress.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Socket socket = null;// 和本线程相关的Socket
public FileService(Socket socket) {
super();
this.socket = socket;
}
public void run() {
OutputStream os = null;
PrintWriter pw = null;
try {
//InputStream is = socket.getInputStream();
// 要完成客户端文件上传到服务器的功能需要将客户机的文件通过FileInputStream进行读取,并包装成BufferedInputStream,
// 将套接字的输出流包装成BufferedOutputStream,用BufferedInputStream中的read()方法读取文件中的数据,
// 并用 BufferedOutputStream中的write()方法进行写入,这样文件就送入了Socket的输出流;
InputStream pathInputStream = socket.getInputStream(); //接收传过来的文件路径
byte[] pathBytes = new byte[2048];
pathInputStream.read(pathBytes);
String path = new String(pathBytes);
path = path.trim();
OutputStream out = socket.getOutputStream();
out.write("200".getBytes()); //
if(path!=null || path.isEmpty()){
InputStream is = socket.getInputStream(); //
// 要完成客户端文件上传到服务器的功能需要将客户机的文件通过FileInputStream进行读取,并包装成BufferedInputStream,
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);// 将BufferedInputStream与套接字的输入流进行连接
File file = new File(path);
file.createNewFile();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(file));
byte[] buf = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);
}
// 强行写入输出流,因为有些带缓冲区的输出流要缓冲区满的时候才输出
bufferedOutputStream.flush(); // 刷新缓冲流
bufferedOutputStream.close(); // 关闭输出流
socket.shutdownInput();// 关闭输入流
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.println("文件已保存至服务器的"+path);
pw.flush();
socket.shutdownOutput();// 关闭输出流
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭相关资源
try {
if (pw != null) {
pw.close();
}
if (os != null) {
os.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileClient {
public static void main(String[] args) {
final String host = "127.0.0.1";
final Integer port = 9999;
String path = "F:/20141120/good.jpg";
File file = new File("F:file/960-2.jpg");
try {
uploadFile(host,port,path,new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static boolean uploadFile(String host, Integer port, String path, InputStream inputStream) {
boolean result = false; // 上传结果
try {
// 1.创建客户端的Socket,指定服务器的IP和端口
Socket socket = new Socket(host, port);
// 2.获取该Socket的输出流,用来向服务器发送文件
OutputStream os = socket.getOutputStream();
os.write(path.getBytes()); //先将文件保存路径传到服务端
InputStream pathRecordInputStream = socket.getInputStream(); //获取返回信息
byte[] pathBytes = new byte[1024];
pathRecordInputStream.read(pathBytes);
String pathRecord = new String(pathBytes);
System.out.println("pathRecord: "+pathRecord.trim());
if("200".equals(pathRecord.trim())){ //如果返回值是200,则服务端接受路径成功
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);// 将BufferedOutputStream与套接字的输出流进行连接
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);// 读取客户机文件
byte[] buf = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);// 向BufferedOutputStream中写入数据
}
bufferedOutputStream.flush();// 刷新缓冲流
socket.shutdownOutput();// 禁用此套接字的输出流
// 3.获取输入流,取得服务器的信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
while ((info = br.readLine()) != null) {
System.out.println("服务器端的信息:" + info);
}
socket.shutdownInput();// 禁用此套接字的输出流
// 4.关闭资源
os.close();
bufferedInputStream.close();
bufferedOutputStream.close();
is.close();
br.close();
}
result = true;
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}