Download presentation
Presentation is loading. Please wait.
1
第十三章 網路檔案傳遞(File Transition)
13-1 簡介 13-2 FileInputStream Class 13-3 FileOutputStream Class 13-4 檔案傳遞(File Translation) 13-5 檔案上傳(File Upload) 13-6 檔案下載(File Download) 13-7 習題(Exercises)
2
13-1 簡介 前章網路訊息傳遞,是為小宗串流傳遞,本章將介紹網路檔案傳遞,是為大宗串流傳遞。除了要使用網路資料串流外,另外要使用本機當地資料串流,讀取檔案內的資料、或將資料寫入檔案。
3
13-2 FileInputStream Class
java.io.FileInputStream繼承(extends) 自InputStream→Object,此類別從檔案建立一個低階輸入串流,使用其中之方法程序讀取檔案內容。
4
13-3 FileOutputStream Class
java.io.FileOutputStream繼承(extends) 自OutputStream→Object,此類別建立一個低階輸出串流,使用其中之方法程序將串流內容寫入指定檔案。
5
13-4 檔案傳遞(File Translation)
設計範例130,於Client端讀取特定檔案的內容,再由網路將內容傳送到Server端之特定檔案內。
6
範例130:設有檔案Server13_4.java、Client13_4.java其功能為解釋檔案傳遞之應用。
檔案Server13_4.java: 01 import java.net.*; 02 import java.io.*; 03 public class Server13_4 { 04 int messagein; 05 static int port; 06 static String outfilename; 07 public Server13_4() { 08 try{ ServerSocket SS = new ServerSocket(port); System.out.println("Server is created and waiting Client to connect..."); Socket socket = SS.accept(); System.out.println("Client IP = " + socket.getInetAddress().getHostAddress());
7
範例130:續1 13 DataInputStream instream = new
DataInputStream(socket.getInputStream()); FileOutputStream fos = new FileOutputStream(outfilename); while(messagein != -1){ messagein = instream.readInt(); fos.write(messagein); } System.out.println("Data written to File successfully!"); 20 } 21 catch(IOException e){ System.out.println(e.getMessage()); 23 } 24 }
8
範例130:續2 25 public static void main(String args[]){
26 if(args.length < 2){ System.out.println("Usage: java Server13_4 [port] [outfilename]"); System.exit(1); 29 } 30 port=Integer.parseInt(args[0]); 31 outfilename = args[1]; 32 Server13_4 ServerStart=new Server13_4(); 33 } 34 }
9
範例130:續3 檔案Client13_4.java: 35 import java.io.*; 36 import java.net.*;
37 public class Client13_4 { 38 int i; 39 static String iaddr; 40 static int port; 41 static String infilename; 42 public Client13_4() { 43 try{ Socket socket=new Socket(InetAddress.getByName(iaddr),port); DataOutputStream outstream = new DataOutputStream(socket.getOutputStream()); FileInputStream fis = new FileInputStream(infilename);
10
範例130:續4 47 while((i=fis.read()) !=-1) outstream.writeInt(i);
System.out.println("Data sent to internet successfully!"); socket.close(); } catch(IOException e){ System.out.println(e.getMessage()); } 55 }
11
範例130:續5 56 public static void main(String args[]) {
57 if (args.length < 3){ System.out.println("USAGE: java Client13_4 [iaddr] [port] [infilename]"); System.exit(1); 60 } 61 iaddr = args[0]; 62 port=Integer.parseInt(args[1]); 63 infilename = args[2]; 64 Client13_4 ClientStart=new Client13_4(); 65 } 66 }
12
13-5 檔案上傳(File Upload) 設計範例131,從Client端將特定檔案由網路上傳(Upload) 到Server端所在目錄內。也即、當要對特定檔案執行網路上傅時,將本例Client.class置於該特定檔案所在目錄,再將本例Server.class置於遠端目的目錄,依執行步驟即可上傳該特定檔案。
13
範例131:設有檔案Server_upload.java、Client_upload.java其功能為解釋檔案上傳之應用。
檔案Server_upload.java: 01 import java.net.*; 02 import java.io.*; 03 public classServer_upload { 04 static int port; 05 static String uploadFile; 06 int messagein; 07 public Server_upload() { 08 try{ ServerSocket SS = new ServerSocket(port); System.out.println("Server_upload is ready..."); Socket cmdsocket = SS.accept(); Socket datasocket = SS.accept();
14
範例131:續1 13 System.out.println("Client IP = " +
cmdsocket.getInetAddress().getHostAddress()); DataInputStream cmdinstream = new DataInputStream(cmdsocket.getInputStream()); uploadFile = cmdinstream.readUTF(); DataInputStream datainstream = new DataInputStream(datasocket.getInputStream()); FileOutputStream fos = new FileOutputStream(uploadFile); while(messagein != -1){ messagein = datainstream.readInt(); fos.write(messagein); } System.out.println("File is uploaded successfully!"); 23 }
15
範例131:續2 24 catch(IOException e){
System.out.println(e.getMessage()); 26 } 27 } 28 public static void main(String args[]){ 29 if(args.length < 1){ System.out.println("Usage: java Server_upload [port]"); System.exit(1); 32 } 33 port = Integer.parseInt(args[0]); 34 Server_upload ServerStart=new Server_upload(); 35 } 36 }
16
範例131:續3 檔案Client_upload.java: 37 import java.io.*;
38 import java.net.*; 39 public class Client_upload { 40 int i; 41 static String iaddr; 42 static int port; 43 static String uploadFile; 44 public Client_upload() { 45 try{ Socket cmdsocket=new Socket(InetAddress.getByName(iaddr),port); Socket datasocket=new Socket(InetAddress.getByName(iaddr),port);
17
範例131:續4 48 DataOutputStream cmdoutstream = new
DataOutputStream(cmdsocket.getOutputStream()); cmdoutstream.writeUTF(uploadFile); DataOutputStream dataoutstream = new DataOutputStream(datasocket.getOutputStream()); FileInputStream fis = new FileInputStream(uploadFile); while((i=fis.read()) !=-1) dataoutstream.writeInt(i); dataoutstream.writeInt(i); System.out.println("File is sent to internet successfully!"); cmdsocket.close(); datasocket.close(); }
18
範例131:續5 58 catch(IOException e){
System.out.println(e.getMessage()); } 61 } 62 public static void main(String args[]) { 63 if (args.length < 3){ System.out.println("USAGE: java Client_upload [iaddr] [port] [uploadFile]"); System.exit(1); 66 } 67 iaddr = args[0]; 68 port=Integer.parseInt(args[1]); 69 uploadFile = args[2]; 70 Client_upload ClientStart=new Client_upload(); 71 } 72 }
19
13-6 檔案下載(File Download) 設計範例132,從Client端對Server端所在目錄內之檔案執行下載(Download)。也即、當要對特定檔案執行網路下載時,將本例Client.class置於需求目錄內,再將本例Server.class置於遠端檔案群目錄內,依執行步驟從Client端對Server端目錄內之特定檔案執行下載。
20
範例132:設有檔案Server_download.java、Client_download.java其功能為解釋檔案下載之應用。
檔案Server_download.java: 01 import java.net.*; 02 import java.io.*; 03 public class Server_download { 04 static int port; 05 static String downloadFile; 06 int messagein, i; 07 public Server_download() { 08 try{ ServerSocket SS = new ServerSocket(port); System.out.println("Server_download is ready...");
21
範例132:續1 11 Socket cmdsocket = SS.accept();
Socket datasocket = SS.accept(); System.out.println("Client IP = " + cmdsocket.getInetAddress().getHostAddress()); DataInputStream cmdinstream = new DataInputStream(cmdsocket.getInputStream()); downloadFile = cmdinstream.readUTF(); DataOutputStream dataoutstream = new DataOutputStream(datasocket.getOutputStream()); FileInputStream fis = new FileInputStream(downloadFile); while((i=fis.read()) !=-1) dataoutstream.writeInt(i); dataoutstream.writeInt(i);
22
範例132:續2 System.out.println("File is sent to internet successfully!"); datasocket.close(); cmdsocket.close(); } catch(IOException e){ System.out.println(e.getMessage()); } 28 } 29 public static void main(String args[]){ 30 if(args.length < 1){ System.out.println("Usage: java Server_download [port]"); System.exit(1); 33 } port=Integer.parseInt(args[0]); Server_download ServerStart=new Server_download(); 36 } 37 }
23
範例132:續3 檔案Client_download.java: 38 import java.io.*;
39 import java.net.*; 40 public class Client_download { 41 int i; 42 static String iaddr; 43 static int port; 44 static String downloadFile; 45 int messagein; 46 public Client_download() { 47 try{ Socket cmdsocket=new Socket(InetAddress.getByName(iaddr),port); Socket datasocket=new Socket(InetAddress.getByName(iaddr),port);
24
範例132:續4 50 DataOutputStream cmdoutstream = new
DataOutputStream(cmdsocket.getOutputStream()); cmdoutstream.writeUTF(downloadFile); DataInputStream datainstream = new DataInputStream(datasocket.getInputStream()); FileOutputStream fos = new FileOutputStream(downloadFile); while(messagein != -1){ messagein = datainstream.readInt(); fos.write(messagein); } System.out.println("File is downloaded successfully!"); cmdsocket.close(); datasocket.close(); }
25
範例132:續5 62 catch(IOException e){
System.out.println(e.getMessage()); } 65 } 66 public static void main(String args[]) { 67 if (args.length < 3){ System.out.println("USAGE: java Client_download [iaddr] [port] [downloadFile]"); System.exit(1); 70 } 71 iaddr = args[0]; 72 port=Integer.parseInt(args[1]); 73 downloadFile = args[2]; 74 Client_download ClientStart=new Client_download(); 75 } 76 }
Similar presentations