您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java怎么利用Socket和IO流實現文件的上傳與下載的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java怎么利用Socket和IO流實現文件的上傳與下載文章都會有所收獲,下面我們一起來看看吧。
1、TCP
2、Socket
3、FileInputStream與FileOutputStream
4、DataInputStream與DataOutputStream
5、多線程
package com.io14; /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ public class Config { public static final String ip = "localhost"; public static final int port = 10088; }
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()); } } }
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 { // 創建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()); } } }
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()); } } }
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()); } } }
關于“Java怎么利用Socket和IO流實現文件的上傳與下載”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java怎么利用Socket和IO流實現文件的上傳與下載”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。