欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java利用Socket和IO流實現(xiàn)文件的上傳與下載

 更新時間:2022年04月12日 15:51:59   作者:谷哥的小弟  
本文主要介紹了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)文章

最新評論