1. Run the program on the client:
The code is as follows: |
Copy code |
Package wtb. khd; Import java. awt. event. ActionEvent; Import java. awt. event. ActionListener; Import java. io. DataOutputStream; Import java. io. File; Import java. io. FileInputStream; Import java. io. OutputStream; Import java.net. Socket; Import javax. swing. JButton; Import javax. swing. JLabel; Import javax. swing. JProgressBar; Public class KHD { Private boolean isnoClose = true; Public boolean isIsnoClose (){ Return isnoClose; } Public void setIsnoClose (boolean isnoClose ){ This. isnoClose = isnoClose; } Public static void main (String args []) { Final KHD khd = new KHD (); String filePath = "D: \ file .zip "; Khd. setIsnoClose (true ); File upload = new File (filePath ); Long fileLength = upload. length (); String fileName = upload. getName (); Try { Socket client = null; Client = new Socket ("localhost", 8888); // "localhost" ---> server IP address OutputStream out = client. getOutputStream (); DataOutputStream dos = new DataOutputStream (out ); Dos. writeUTF ("file name prefix _" + fileName ); Dos. writeLong (fileLength ); FileInputStream FCM = new FileInputStream (upload ); Byte [] buffer = new byte [2048]; Int len = 0; Double scbl = 0; // File upload ratio Int scdx = 0; // File upload size Final JinDuBar jdb = new JinDuBar ("file upload", System. getProperty ("user. dir") + "\ bin \ image \ jindutiao.gif "); String [] uploadInfo = new String [] {"uploading File:" + filePath, "File name:" + fileName, "Size of the uploaded File:" + fileLength + "byte", "uploaded: 0 bytes", "upload ratio: 0% "}; JLabel [] labels = jdb. getLabels (); For (int lxb = 0; lxb <labels. length; lxb ++ ){ Labels [lxb]. setText (uploadInfo [lxb]); } JProgressBar p = jdb. getProgress (); JButton closeBtn = jdb. getCloseBtn (); CloseBtn. addActionListener (new ActionListener (){ Public void actionreceivmed (ActionEvent AE ){ Jdb. dispose (); Khd. setIsnoClose (false ); } }); While (len = FCM. read (buffer)> 0 & khd. isnoClose ){ Thread. sleep (100 ); Scdx + = len; Scbl = (double) scdx/fileLength; Int bl = (int) (scbl * 100 ); Dos. write (buffer, 0, len ); P. setValue (bl ); P. setString (bl + "% "); String [] gxsc = new String [] {"uploaded:" + scdx + "byte", "upload ratio:" + bl + "% "}; For (int lxb = 0; lxb <gxsc. length; lxb ++ ){ Labels [lxb + 3]. setText (gxsc [lxb]); } } Jdb. dispose (); Out. close (); FCM. close (); Client. close (); } Catch (Exception e1 ){ E1.printStackTrace (); } } } |
/****************************/
2. Progress bar
The code is as follows: |
Copy code |
Package wtb. util; Import java. awt. BorderLayout; Import java. awt. Color; Import java. awt. Cursor; Import java. awt. Dimension; Import java. awt. GridLayout; Import javax. swing. ImageIcon; Import javax. swing. JButton; Import javax. swing. JLabel; Import javax. swing. JPanel; Import javax. swing. JProgressBar; Import javax. swing. JWindow; @ SuppressWarnings ("serial ") Public class JinDuBar extends JWindow { Private JProgressBar progress; Private JButton btn; Private JLabel [] labels = new JLabel [5]; Private JButton closeBtn; Public JProgressBar getProgress (){ Return progress; } Public void setProgress (JProgressBar progress ){ This. progress = progress; } Public JButton getBtn (){ Return btn; } Public void setBtn (JButton btn ){ This. btn = btn; } Public JLabel [] getLabels (){ Return labels; } Public void setLabels (JLabel [] labels ){ This. labels = labels; } Public JButton getCloseBtn (){ Return closeBtn; } Public void setCloseBtn (JButton closeBtn ){ This. closeBtn = closeBtn; } Public JinDuBar (String title, String bgImg ){ SetAlwaysOnTop (true ); SetCursor (Cursor. getPredefinedCursor (Cursor. WAIT_CURSOR )); JPanel splash = new JPanel (new BorderLayout ()); JPanel top = new JPanel (); Top. setBackground (new Color (255,153,204 )); BorderLayout toplay = new BorderLayout (); Top. setLayout (toplay ); JLabel tt = new JLabel (title ); ImageIcon cloImg = new ImageIcon (System. getProperty ("user. dir") + "/bin/image/4.png "); CloseBtn = new JButton ("close", cloImg ); CloseBtn. setBackground (new Color (255,153,204 )); Top. add (tt, BorderLayout. CENTER ); Top. add (closeBtn, BorderLayout. EAST ); Top. add (new JLabel (""), BorderLayout. WEST ); Splash. add (top, BorderLayout. NORTH ); ImageIcon img = new ImageIcon (bgImg ); Btn = new JButton (img ); GetLayeredPane (). add (btn, new Integer (Integer. MIN_VALUE )); Btn. setBounds (0, 0, img. getIconWidth (), img. getIconHeight ()); GridLayout gl = new GridLayout (this. getLabels (). length, 1); // labels. length row 1 column Btn. setLayout (gl ); For (int I = 0; I <labels. length; I ++ ){ This. getLabels () [I] = new JLabel ("000" + I ); Btn. add (labels [I]); } Splash. add (btn, BorderLayout. CENTER ); Progress = new JProgressBar (1,100 ); Progress. setStringPainted (true ); Progress. setBorderPainted (false ); Progress. setString ("0% "); Progress. setBackground (Color. WHITE ); Splash. add (progress, BorderLayout. SOUTH ); SetContentPane (splash ); Dimension screen = getToolkit (). getScreenSize (); SetSize (img. getIconWidth (), img. getIconHeight () + 60 ); SetLocation (screen. width-getSize (). width)/2, (screen. height-getSize (). height)/2 ); New DragJWindow (this, splash); // you can drag the setting window. SetVisible (true ); } }
|
/********************************/
3. Set the Window to be dragged
The code is as follows: |
Copy code |
Package wtb. util; Import javax. swing. SwingUtilities; Import java. awt. Component; Import java. awt. Point; Import java. awt. Window; Import java. awt. event. MouseAdapter; Import java. awt. event. MouseEvent; Import java. awt. event. MouseListener; Import java. awt. event. MouseMotionAdapter; Public class DragJWindow { Private Window fWindow; Private Component fComponent; Private int dX; Private int dY; Public DragJWindow (Window window, Component component ){ FWindow = window; FComponent = component; FComponent. addMouseListener (createMouseListener ()); FComponent. addMouseMotionListener (createMouseMotionListener ()); } Private MouseListener createMouseListener (){ Return new MouseAdapter (){ @ Override Public void mousePressed (MouseEvent e ){ Point clickPoint = new Point (e. getPoint ()); SwingUtilities. convertPointToScreen (clickPoint, fComponent ); DX = clickPoint. x-fWindow. getX (); DY = clickPoint. y-fWindow. getY (); } }; } Private MouseMotionAdapter createMouseMotionListener (){ Return new MouseMotionAdapter (){ @ Override Public void mouseDragged (MouseEvent e ){ Point dragPoint = new Point (e. getPoint ()); SwingUtilities. convertPointToScreen (dragPoint, fComponent ); FWindow. setLocation (dragPoint. x-dX, dragPoint. y-dY ); } }; } }
|
/*************************************** ***/
/*************************************** ***/
Server:
1. Run the program on the server
The code is as follows: |
Copy code |
Package wtb. fwq; Import java.net. ServerSocket; Import java.net. Socket; Import wtb. fwq. UploadThread; Public class UploadServer { Public final static String fileDir = "E: \ wtb "; Public static void main (String args []) throws Exception { ServerSocket server = null; Socket client = null; Server = new serversockets (8888 ); While (true ){ Client = server. accept (); // Set a thread for each user UploadThread uft = new UploadThread (client, fileDir ); New Thread (uft). start (); } } } |
/*************************************/
2. Set a thread class for each user
The code is as follows: |
Copy code |
Package wtb. fwq; Import java. io. DataInputStream; Import java. io. File; Import java. io. FileOutputStream; Import java. io. InputStream; Import java.net. Socket; Public class UploadThread implements Runnable { Private Socket client; Private String fileDir; Private String fileName; Public UploadThread (Socket client, String fileDir ){ This. client = client; This. fileDir = fileDir; } Public void run (){ InputStream in = null; FileOutputStream fos = null; Try { In = client. getInputStream (); DataInputStream dis = new DataInputStream (in ); FileName = dis. readUTF (); Long fileLength = dis. readLong (); Long xzdx = 0; Fos = new FileOutputStream (fileDir + File. separator + fileName ); Byte [] buffer = new byte [1024]; Int len = 0; While (len = dis. read (buffer)> 0) { Fos. write (buffer, 0, len ); Xzdx + = len; } Fos. flush (); Fos. close (); In. close (); Client. close (); If (xzdx! = FileLength) {// if the file has not been uploaded, delete the file uploaded to the server. File f = new File (fileDir + File. separator + fileName ); F. delete (); } } Catch (Exception e ){ System. out. println ("exception !!!! "); } } } /************************/ |
Running result view: