The FileUpload package has three classes:
OpenAndSave. java
TCPClient. java
TCPServer. java
1. OpenAndSave. java
Package FileUpload; import java. awt. *; import javax. swing. *; import java. util. *; import java. io. *; public class OpenAndSave extends JFrame {// select Open File public String getFile () {String fileName = ""; FileDialog fd = new FileDialog (this, "select the file to be passed to zpc", FileDialog. LOAD); fd. setDirectory ("C: \"); fd. setVisible (true); // obtain the file selected in the dialog box if (fd. getFile ()! = Null) {fileName = fd. getDirectory () + fd. getFile (); System. out. println ("opened" + fileName);} return fileName;} // Save the public OutputStream saveFile (String fileName) {OutputStream OS = null; try {FileDialog fd = new FileDialog (this, "save file", FileDialog. SAVE); // The default directory fd is displayed when the dialog box is opened. setDirectory ("C: \"); fd. setFile (fileName); fd. setVisible (true); if (fd. getFile ()! = Null) {File myFile = new File (fd. getDirectory (), fd. getFile (); OS = new FileOutputStream (myFile) ;}} catch (IOException ioe) {JOptionPane. showMessageDialog (this, "An error occurred while saving the file, stopped") ;}return OS ;}}
2. TCPServer. java
/** Author: zpc * Time: 2014-5-1 */package FileUpload; import java. io. *; import java.net. *; import java. util. random; import javax. swing. *; // each time a client request is received, a single thread is opened to process class UploadProcess implements Runnable {private Socket s; public UploadProcess (Socket s) {this. s = s;} public void run () {OutputStream fos = null; try {DataInputStream FD = new DataInputStream (s. getInputStream (); // unlike the copy object, the network upload object is an I/O Stream object obtained from the Socket. String fileName = Sox. readUTF (); DataOutputStream fos1 = new DataOutputStream (s. getOutputStream (); // write back information; if (JOptionPane. showOptionDialog (null, "Do you want to accept Files" + fileName +? "," Message ", JOptionPane. YES_NO_OPTION, 0, null) = 0) {System. out. println (" I have selected to accept the file! "); String message1 =" received by the server! "; Fos1.writeUTF (message1); fos = new OpenAndSave (). saveFile (fileName); int len = 0; byte [] bytes = new byte [1024]; if (FS! = Null) {while (len = Fi. read (bytes ))! =-1) {fos. write (bytes, 0, len) ;}} JOptionPane. showMessageDialog (null, "the file is saved successfully! "); If (FS! = Null) FCM. close (); if (fos! = Null) fos. close ();} else {System. out. println ("I chose to reject it! "); String message2 =" prompt: the recipient has refused to accept it! "; Fos1.writeUTF (message2) ;}} catch (Exception e) {System. out. println (" failed to accept the file! ") ;}} Public class TCPServer {public static void main (String [] args) throws IOException {// create a server socket object ServerSocketServerSocket ss = new ServerSocket (10001 ); while (true) {Socket s = ss. accept (); // create a Socket object for each client request, and open a single Thread to process new Thread (new UploadProcess (s )). start ();}}}
3. TCPClient. java
/** Author: zpc * Function: upload a file to zpc * Time: 2014-5-1 */package FileUpload; import java. io. *; import java.net. *; public class TCPClient {public static void main (String [] args) throws UnknownHostException {// read the file. data Source OpenAndSave open = new OpenAndSave (); String fileName = open. getFile (); File myFile = new File (fileName); InputStream in = null; try {in = new FileInputStream (myFile);} catch (FileNotFoundEx Ception e1) {System. out. println ("unselected file! ");} // The data destination is another host. Obtain the I/O stream if (in! = Null) {Socket s = null; try {s = new Socket ("127.0.0.1", 10001 ); // write the IP address of the target machine and the port number DataOutputStream OS = new DataOutputStream (s. getOutputStream (); OS. writeUTF (fileName); byte [] B = new byte [1024]; int len = 0; while (len = in. read (B ))! =-1) {OS. write (B, 0, len);} // System. out. println ("message"); DataInputStream FD = new DataInputStream (s. getInputStream (); String message = Fi. readUTF (); System. out. println (message); if (OS! = Null) OS. close (); if (in! = Null) in. close ();} catch (Exception e) {System. out. println ("it seems that zpc is not online. Upload it later !! ") ;}} Else {System. out. println (" failed to read the file (in = null )! ");}}}