首頁 > 軟體

Java利用Socket和IO流實現檔案的上傳與下載

2022-04-12 19:01:14

背景概述

本文利用Socket程式設計和IO流技術實現檔案的上傳與下載。

核心技術

  • 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 {
			// 建立待上傳檔案物件
			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();
	}

	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());
		}
	}

}

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());
		}
	}

}

到此這篇關於Java利用Socket和IO流實現檔案的上傳與下載的文章就介紹到這了,更多相關Java 檔案上傳與下載內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com