java socket 文件上传下载 原创
socket 文件服务端
public class FileServer extends Thread {
// 文件的保存路径
private String fileDir;
// socket服务器端口号
private int port;
// 是否停止
private boolean stop;
public String getFileDir() {
return fileDir;
}
public void setFileDir(String fileDir) {
this.fileDir = fileDir;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public static void main(String[] args) {
FileServer fd = new FileServer();
fd.setFileDir("d:\\");
fd.setPort(9005);
fd.start();
}
/**
* 文件下载
*/
@Override
public void run() {
Socket socket = null;
try {
ServerSocket ss = new ServerSocket(port);
do {
socket = ss.accept();
// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
System.out.println("建立socket链接");
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
// 本地保存路径,文件名会自动从服务器端继承而来。
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
long passedlen = 0;
long len = 0;
// 获取文件名
String file = fileDir + inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
len = inputStream.readLong();
System.out.println("文件的长度为:" + len + "\n");
System.out.println("开始接收文件!" + "\n");
while (true) {
int read = 0;
read = inputStream.read(buf);
passedlen += read;
if (read == -1) {
break;
}
// 下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
System.out.println("文件接收了" + (passedlen * 100 / len) + "%\n");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成,文件存为" + file + "\n");
fileOut.close();
} while (!stop);
} catch (Exception e) {
System.out.println("接收消息错误" + "\n");
e.printStackTrace();
}
}
}
socket 文件上传客户端
public class FileUpClient {
// 上传的文件路径
private String filePath;
// socket服务器地址和端口号
private String host;
private int port;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public static void main(String[] args) {
FileUpClient fu = new FileUpClient();
fu.setHost("127.0.0.1");
fu.setPort(9005);
fu.setFilePath("d:\\tmp\\");
fu.uploadFile("123.zip");
}
/**
* 客户端文件上传
*
* @param fileName 文件名
*/
public void uploadFile(String fileName) {
Socket s = null;
try {
s = new Socket(host, port);
// 选择进行传输的文件
File fi = new File(filePath + fileName);
System.out.println("文件长度:" + (int) fi.length());
DataInputStream fis = new DataInputStream(new FileInputStream(filePath + fileName));
DataOutputStream ps = new DataOutputStream(s.getOutputStream());
// 将文件名及长度传给客户端。这里要真正适用所有平台,例如中文名的处理,还需要加工,具体可以参见Think In Java 4th里有现成的代码。
//design pattern 设计模式 <<Head First Design Pattern>><<Head First 设计模式>><<Head First Java>>
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
// 直到socket超时,导致数据不完整。
fis.close();
ps.close();
s.close();
System.out.println("文件传输完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文来自博客园,作者:清霜辰,转载请注明原文链接:https://www.cnblogs.com/cnjim/p/18443533
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了