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)建待上傳文件對象 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對象 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對象 Socket socket = new Socket(Config.ip, Config.port); // 從Socket獲取InputStream InputStream inputStream = socket.getInputStream(); // 創(chuàng)建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; // 創(chuàng)建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(); } public void handleUploadFile() { try { // 創(chuàng)建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 { // 創(chuàng)建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(); // 創(chuàng)建DataInputStream對象 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對象 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)建待下載文件對象 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot自動裝配之注入DispatcherServlet的實(shí)現(xiàn)方法
這篇文章主要介紹了Springboot自動裝配之注入DispatcherServlet,Springboot向外界提供web服務(wù),底層依賴了springframework中的web模塊來實(shí)現(xiàn),那么springboot在什么時機(jī)向容器注入DispatcherServlet這個核心類的呢?帶著這個問題一起通過本文學(xué)習(xí)吧2022-05-05java實(shí)現(xiàn)輕量型http代理服務(wù)器示例
這篇文章主要介紹了java實(shí)現(xiàn)輕量型http代理服務(wù)器示例,需要的朋友可以參考下2014-04-04SpringBoot為啥不用配置啟動類的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot為啥不用配置啟動類的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11SpringBoot+MinIO實(shí)現(xiàn)文件切片極速詳解
在現(xiàn)代Web應(yīng)用中,文件上傳是一個常見的需求,尤其是對于大文件的上傳,如視頻、音頻或大型文檔,所以本文就來為大家介紹一下如何使用Spring Boot和MinIO實(shí)現(xiàn)文件切片極速上傳技術(shù)吧2023-12-12idea導(dǎo)入springboot項目沒有maven的解決
這篇文章主要介紹了idea導(dǎo)入springboot項目沒有maven的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Spring Boot利用@Async異步調(diào)用:使用Future及定義超時詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2018-05-05