Java利用Socket和IO流實現(xiàn)文件的上傳與下載
背景概述
本文利用Socket編程和IO流技術(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流實現(xiàn)文件的上傳與下載的文章就介紹到這了,更多相關(guān)Java 文件上傳與下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot自動裝配之注入DispatcherServlet的實現(xiàn)方法
這篇文章主要介紹了Springboot自動裝配之注入DispatcherServlet,Springboot向外界提供web服務(wù),底層依賴了springframework中的web模塊來實現(xiàn),那么springboot在什么時機向容器注入DispatcherServlet這個核心類的呢?帶著這個問題一起通過本文學習吧2022-05-05java實現(xiàn)輕量型http代理服務(wù)器示例
這篇文章主要介紹了java實現(xiàn)輕量型http代理服務(wù)器示例,需要的朋友可以參考下2014-04-04SpringBoot+MinIO實現(xiàn)文件切片極速詳解
在現(xiàn)代Web應(yīng)用中,文件上傳是一個常見的需求,尤其是對于大文件的上傳,如視頻、音頻或大型文檔,所以本文就來為大家介紹一下如何使用Spring Boot和MinIO實現(xiàn)文件切片極速上傳技術(shù)吧2023-12-12Spring Boot利用@Async異步調(diào)用:使用Future及定義超時詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:使用Future及定義超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友可以參考下2018-05-05