SpringBoot实现扫码登录的示例代码
412
2022-08-04
Java利用Socket和IO流实现文件的上传与下载(java socket文件)
目录背景概述核心技术ConfigClientServerUploadRunnableImplDownloadRunnableImpl
背景概述
本文利用Socket编程和IO流技术实现文件的上传与下载。
核心技术
1、TCP2、Socket3、FileInputStream与FileOutputStream4、DataInputStream与DataOutputStream5、多线程
Config
package com.io14;
/**
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Config {
public static final String ip = "localhost";
public static final int port = 10088;
}
Client
package com.io14;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Client {
public static void main(String arg[]) {
testDownload();
}
public static void testUpload() {
String filePath = "D:" + File.separator + "beauty.jpg";
Client client = new Client();
client.uploadFile(filePath);
}
public static void testDownload() {
Client client = new Client();
client.downloadFile();
}
public void uploadFile(String filePath) {
try {
// 创建待上传文件对象
File file = new File(filePath);
String fileName = file.getName();
long fileLength = file.length();
System.out.println("客户端待上传文件:" + fileName + ",其大小为:" + fileLength);
// 创建FileInputStream
FileInputStream fileInputStream = new FileInputStream(filePath);
// 创建Socket对象
Socket socket = new Socket(Config.ip, Config.port);
// 从Socket获取OutputStream
OutputStream outputStream = socket.getOutputStream();
// 创建DataOutputStream
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
// 利用DataOutputStream写出文件名和文件大小
dataOutputStream.writeUTF(fileName);
dataOutputStream.writeLong(fileLength);
dataOutputStream.flush();
// IO流读写操作
byte[] buf = new byte[1024 * 1];
int len = 0;
while ((len = fileInputStream.read(buf)) != -1) {
dataOutputStream.write(buf, 0, len);
}
// 释放资源
dataOutputStream.flush();
fileInputStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void downloadFile() {
try {
// 创建Socket对象
Socket socket = new Socket(Config.ip, Config.port);
// 从Socket获取InputStream
InputStream inputStream = socket.getInputStream();
// 创建DataInputStream对象
DataInputStream dataInputStream = new DataInputStream(inputStream);
// 获取下载文件的文件名和文件大小
String fileName = dataInputStream.readUTF();
long fileLength = dataInputStream.readLong();
System.out.println("客户端下载文件:" + fileName + ",其大小为:" + fileLength);
// 组拼文件保存路径
String fileDir = "D:";
String filePath = fileDir + File.separator + fileName;
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
// IO流读写操作
byte[] buf = new byte[1024 * 1];
int len = 0;
while ((len = dataInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
// 释放资源
fileOutputStream.flush();
fileOutputStream.close();
dataInputStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
Server
package com.io14;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Server {
public static void main(String arg[]) {
testDownload();
}
public static void testUpload() {
Server server = new Server();
server.handleUploadFile();
}
public static void testDownload() {
Server server = new Server();
server.handleDownloadFile();
}
publhttp://ic void handleUploadFile() {
try {
// 创建ServerSocket对象
ServerSocket serverSocket = new ServerSocket(Config.port);
while (true) {
// 接收客户端连接请求
Socket socket = serverSocket.accept();
// 开启子线程处理文件上传
UploadRunnableImpl uploadRunnableImpl = new UploadRunnableImpl(socket);
Thread thread = new Thread(uploadRunnableImpl);
thread.start();
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void handleDownloadFile() {
try {
// 创建ServerSocket对象
ServerSocket serverSocket = new ServerSocket(Config.port);
while (true) {
// 接收客户端连接请求
Socket socket = serverSocket.accept();
// 开启子线程处理文件下载
DownloadRunnableImpl downloadRunnableImpl = new DownloadRunnableImpl(socket);
Thread thread = new Thread(downloadRunnableImpl);
thread.start();
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
UploadRunnableImpl
package com.io14;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
/**
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class UploadRunnableImpl implements Runnable {
private Socket socket;
public UploadRunnableImpl(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 从Socket获取InputStream
InputStream inputStream = socket.getInputStream();
// 创建DataInputStream对象
DataInputStream dataInputStream = new DataInputStream(inputStream);
// 获取上传文件的文件名和文件大小
String fileName = dataInputStream.readUTF();
long fileLength = dataInputStream.readLong();
System.out.println("服务端接收上传文件:"+fileName+",其大小为:"+fileLength);
// 组拼文件保存路径
String fileDir = "E:";
String filePath= fileDir + File.separator+fileName;
// 创建FileOutputStream对象
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
// IO流读写操作
byte[] buf = new byte[1024*1];
int len = 0;
while ((len = dataInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
// 释放资源
fileOutputStream.flush();
fileOutputStream.close();
dataInputStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
DownloadRunnableImpl
package com.io14;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class DownloadRunnableImpl implements Runnable{
private Socket socket;
public DownloadRunnableImpl(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// 创建待下载文件对象
String filePath = "E:"+File.separator+"beauty.jpg";
File file = new File(filePath);
String fileName = file.getName();
long fileLength = file.length();
System.out.println("服务端待下载文件:" + fileName + ",其大小为:" + fileLength);
// 创建FileInputStream
FileInputStream fileInputStream = new FileInputStream(filePath);
// 从Socket获取OutputStream
OutputStream outputStream = socket.getOutputStream();
// 创建DataOutputStream
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
// 利用DataOutputStream写出文件名和文件大小
dataOutputStream.writeUTF(fileName);
dataOutputStream.writeLong(fileLength);
dataOutputStream.flush();
// IO流读写操作
byte[] buf = new byte[1024 * 1];
int len = 0;
while ((len = fileInputStream.read(buf)) != -1) {
dataOutputStream.write(buf, 0, len);
}
// 释放资源
dataOutputStream.flush();
fileInputStream.close();
socket.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~