Java通过Socket编程实现文件传输
客户端:
package com.yt.socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientSocket {
public static final String fileDir = "D:/";
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "1.txt";
String filePath = fileDir + fileName;
System.out.println("send filePath = " + filePath);
Socket socket = null;
try {
socket = new Socket(TCPServerSocket.IP, TCPServerSocket.PORT);
if(socket != null){
System.out.println("Connection succcessed !");
sendFile(socket, filePath);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendFile(Socket socket, String filePath){
DataOutputStream dos = null;
BufferedInputStream bis = null; //What can BufferedInputStream help ?
// FileInputStream fis = null;
byte[] bytes = new byte[1024];
try {
try {
/*
* new a File with the filePath
* new a FileInputStream with the File to read the file by byte
* new a BufferedInputStream with the FileInputStream to use buffered stream
*/
bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
//首先发送文件名 客户端发送使用writeUTF方法,服务器端应该使用readUTF方法
dos.writeUTF(getFileName(filePath));
//之后再发送文件的内容
int length = 0;
while((length = bis.read(bytes, 0, bytes.length)) > 0){
dos.write(bytes, 0, length);
dos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//使用完毕后,应关闭输入、输出流和socket
if(bis != null) bis.close();
if(dos != null) dos.close();
if(socket != null) socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* example:
* filePath = "D:/Java/workspace/TCPSocketFileTransfer/Java_TCPIP_Socket.pdf"
* return "Java_TCPIP_Socket.pdf"
*/
private static String getFileName(String filePath){
String[] parts = filePath.split("/");
return parts[parts.length - 1];
}
}
服务器端:
package com.yt.socket;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
public class TCPServerSocket {
public static final int PORT = 8887; //port
public static final String IP = "127.0.0.1"; //my ip address
/**
* @param args
*/
public static void main(String[] args) {
ServerSocket mServerSocket = null;
Socket socket = null;
try {
//创建服务器端
mServerSocket = new ServerSocket(PORT);
System.out.println("Server is already created now ! Waiting for client to connect ...");
//客户端连接服务器端
socket = mServerSocket.accept();
System.out.println("One client connected to this server successfully !");
//连接成功,开始传输文件
receiveFile(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void receiveFile(Socket socket) {
FileOutputStream fos = null;
DataInputStream dis = null;
//buffer起缓冲作用,一次读取或写入多个字节的数据
byte[] buffer = new byte[1024];
try {
try {
//这里使用DataInputStream,可以调用它的readUTF方法来读取要传输的文件名,客户端使用writeUTF方法将文件名先传输过来
dis = new DataInputStream(socket.getInputStream());
//首先读取文件名
String oldFileName = dis.readUTF();
//文件路径采用与客户端相同的路径,文件名重新命名
String filePath = TCPClientSocket.fileDir + genereateFileName(oldFileName);
System.out.println("receive filePath = " + filePath);
//利用FileOutputStream来操作文件输出流
fos = new FileOutputStream(new File(filePath));
int length = 0;
/*
* length = dis.read(buffer, 0, buffer.length) 一次读入1024个字节的内容到buffer中,length代表实际读入的字节数
* fos.write(buffer, 0, length) 一次从buffer中的length个字节的内容写入到文件中
* (注:文件大小超过1024B时,length一般为1024,最后一次读取可能小于1024)
*/
while((length = dis.read(buffer, 0, buffer.length)) > 0){
fos.write(buffer, 0, length);
fos.flush();
}
} finally {
//使用完毕后,应关闭输入、输出流和socket
if(dis != null) dis.close();
if(fos != null) fos.close();
if(socket != null) socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* example :
* oldName = "Java_TCPIP_Socket.pdf"
* newName = "Java_TCPIP_Socket-2.pdf"
*/
private static String genereateFileName(String oldName){
String newName = null;
newName = oldName.substring(0, oldName.lastIndexOf(".")) + "-2" + oldName.substring(oldName.lastIndexOf("."));
return newName;
}
}