Java網絡編程——TCP圖片上傳


1、編寫一個服務器端程序,用來接收圖片。創建一個監聽指定端口號的ServerSocket服務端對象,在while(true)無限循環中持續調用ServerSocket的accept()方法來接收客戶端的請求,每當和一個客戶端建立連接后,就開啟一個新的線程和這個客戶端進行交互。在處理文件上傳功能的ServerThread線程管理類中,先進行客戶端文件上傳保存處理,確定了文件保存目錄和文件命名方式,然后進行保存處理,最后又通過客戶端輸出流,向客戶端輸出“上傳成功”的信息進行響應。從運行結果可以看出,服務器端進入阻塞狀態,等待客戶端連接。
01

相關代碼

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class ServerThread implements Runnable {
	private Socket socket;                 
	public ServerThread(Socket socket) { 
		this.socket = socket;
	}
	public void run() {
		String ip = socket.getInetAddress().getHostAddress();
		int count = 1;                                                
		try {
			File parentFile = new File("D:\\upload\\");   
			if (!parentFile.exists()) {                      
				parentFile.mkdir();
			}
			File file = new File(parentFile, ip + "(" + count + ").jpg");
			while  (file.exists()) {
				file = new File(parentFile, ip + "(" + (count++) + ").jpg");
			}
			InputStream in = socket.getInputStream();
			FileOutputStream fos = new FileOutputStream(file); 
			byte[] buf = new byte[1024]; 
			int len = 0; 
			while ((len = in.read(buf)) != -1) { 
				fos.write(buf, 0, len);  
			}
			OutputStream out = socket.getOutputStream();
			out.write("上傳成功".getBytes());  
			in.close();
			fos.close();  
			socket.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

public class UploadTCPServer {
	public static void main(String[] args) throws Exception {
		@SuppressWarnings("resource")
		ServerSocket serverSocket = new ServerSocket(10001);  
		while (true) {  
			Socket client = serverSocket.accept();	
			new Thread(new ServerThread(client)).start();
		}
	}
}

2、創建了Socket對象,指定連接服務器的IP地址和端口號,然后分別通過Socket的輸出流對象向服務端上傳圖片和輸入流對象獲取服務端響應信息。其中,向服務端上傳的源圖片地址為D:\1.jpg,即D盤根目錄下的1.jpg圖片,在上傳之前一定要確保圖片存在。另外,在向服務端上傳完畢圖片后要調用Socket的shutDownOutput()方法。關閉客戶端的輸出流。

02

相關代碼

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class UploadTCPClient {
	public static void main(String[] args) throws Exception {
		Socket client = new Socket(InetAddress.getLocalHost(),10001);
		OutputStream out = client.getOutputStream();
		FileInputStream fis = new FileInputStream("D:\\1.jpg");
		byte[] buf = new byte[1024]; 
		int len= 0; 
		System.out.println("連接到服務器端,開始文件上傳!");
		while ((len = fis.read(buf)) != -1) { 
			out.write(buf, 0, len);
		}
		client.shutdownOutput(); 
		InputStream is = client.getInputStream();           
		byte[] bufMsg= new byte[1024];                      
		int len2 = is.read(bufMsg);
		while (len2 != -1){
			System.out.println(new String(bufMsg, 0, len2)); 
			len2 = is.read(bufMsg);
		}
		out.close();
		is.close();
		fis.close();                                             
		client.close();                                           
	}
}

3、運行結果如下:

03
04


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM