Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載
背景概述
本文利用Socket編程和IO流技術(shù)實(shí)現(xiàn)文件的上傳與下載。
核心技術(shù)
- 1、TCP
- 2、Socket
- 3、FileInputStream與FileOutputStream
- 4、DataInputStream與DataOutputStream
- 5、多線程
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 {
// 創(chuàng)建待上傳文件對(duì)象
File file = new File(filePath);
String fileName = file.getName();
long fileLength = file.length();
System.out.println("客戶端待上傳文件:" + fileName + ",其大小為:" + fileLength);
// 創(chuàng)建FileInputStream
FileInputStream fileInputStream = new FileInputStream(filePath);
// 創(chuàng)建Socket對(duì)象
Socket socket = new Socket(Config.ip, Config.port);
// 從Socket獲取OutputStream
OutputStream outputStream = socket.getOutputStream();
// 創(chuàng)建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 {
// 創(chuàng)建Socket對(duì)象
Socket socket = new Socket(Config.ip, Config.port);
// 從Socket獲取InputStream
InputStream inputStream = socket.getInputStream();
// 創(chuàng)建DataInputStream對(duì)象
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;
// 創(chuàng)建FileOutputStream對(duì)象
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();
}
public void handleUploadFile() {
try {
// 創(chuàng)建ServerSocket對(duì)象
ServerSocket serverSocket = new ServerSocket(Config.port);
while (true) {
// 接收客戶端連接請(qǐng)求
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 {
// 創(chuàng)建ServerSocket對(duì)象
ServerSocket serverSocket = new ServerSocket(Config.port);
while (true) {
// 接收客戶端連接請(qǐng)求
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();
// 創(chuàng)建DataInputStream對(duì)象
DataInputStream dataInputStream = new DataInputStream(inputStream);
// 獲取上傳文件的文件名和文件大小
String fileName = dataInputStream.readUTF();
long fileLength = dataInputStream.readLong();
System.out.println("服務(wù)端接收上傳文件:"+fileName+",其大小為:"+fileLength);
// 組拼文件保存路徑
String fileDir = "E:";
String filePath= fileDir + File.separator+fileName;
// 創(chuàng)建FileOutputStream對(duì)象
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 {
// 創(chuàng)建待下載文件對(duì)象
String filePath = "E:"+File.separator+"beauty.jpg";
File file = new File(filePath);
String fileName = file.getName();
long fileLength = file.length();
System.out.println("服務(wù)端待下載文件:" + fileName + ",其大小為:" + fileLength);
// 創(chuàng)建FileInputStream
FileInputStream fileInputStream = new FileInputStream(filePath);
// 從Socket獲取OutputStream
OutputStream outputStream = socket.getOutputStream();
// 創(chuàng)建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());
}
}
}
到此這篇關(guān)于Java利用Socket和IO流實(shí)現(xiàn)文件的上傳與下載的文章就介紹到這了,更多相關(guān)Java 文件上傳與下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot 任務(wù)調(diào)度動(dòng)態(tài)設(shè)置方式(不用重啟服務(wù))
這篇文章主要介紹了SpringBoot 任務(wù)調(diào)度 動(dòng)態(tài)設(shè)置方式(不用重啟服務(wù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
在Java的Struts中判斷是否調(diào)用AJAX及用攔截器對(duì)其優(yōu)化
這篇文章主要介紹了在Java的Struts中判斷是否調(diào)用AJAX及用攔截器對(duì)其優(yōu)化的方法,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01
基于Java8 Stream API實(shí)現(xiàn)數(shù)據(jù)抽取收集
這篇文章主要介紹了基于Java8 Stream API實(shí)現(xiàn)數(shù)據(jù)抽取收集,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP軟件主界面(4)
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件,F(xiàn)TP軟件主界面編寫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
SpringBoot整合jasypt實(shí)現(xiàn)重要數(shù)據(jù)加密
Jasypt是一個(gè)專注于簡(jiǎn)化Java加密操作的開源工具,這篇文章主要為大家介紹了詳細(xì)介紹了如何使用jasypt實(shí)現(xiàn)重要數(shù)據(jù)加密,感興趣的小伙伴可以了解下2025-03-03
SpringBoot Session接口驗(yàn)證實(shí)現(xiàn)流程詳解
這篇文章主要介紹了SpringBoot+Session實(shí)現(xiàn)接口驗(yàn)證(過(guò)濾器+攔截器)文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
springboot項(xiàng)目test文件夾下帶main方法的類不能運(yùn)行問(wèn)題
這篇文章主要介紹了springboot項(xiàng)目test文件夾下帶main方法的類不能運(yùn)行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

