Java Socket file transmission

Source: Internet
Author: User

Import java. io. dataOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java.net. inetSocketAddress; import java.net. socket; /*** File Sending client main program * @ author admin_Hzw **/public class BxClient {/*** main method of the Program * @ param args * @ throws IOException */public static void main (String [] args) throws IOException {int length = 0; double sumL = 0; byte [] sendByt Es = null; Socket socket = null; DataOutputStream dos = null; FileInputStream FCM = null; boolean bool = false; try {File file = new File ("D:/day. Zip "); // The file path to be transmitted long l = file. length (); socket = new Socket (); socket. connect (new InetSocketAddress ("127.0.0.1", 48123); dos = new DataOutputStream (socket. getOutputStream (); FCM = new FileInputStream (file); sendBytes = new byte [1024]; while (length = Fiis. read (sendBytes, 0, sendBytes. length)> 0) {sumL + = length; System. out. println ("transmitted:" + (sumL/l) * 100) + "%"); dos. write (sendBytes, 0, length); dos. flush ();} // although the data types are different, JAVA will automatically convert to the same data type and then compare if (sumL = l) {bool = true ;}} catch (Exception e) {System. out. println ("client file transmission exception"); bool = false; e. printStackTrace ();} finally {if (dos! = Null) dos. close (); if (FS! = Null) FCM. close (); if (socket! = Null) socket. close ();} System. out. println (bool? "Successful": "failed") ;}} server: [java] import java. io. dataInputStream; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java.net. serverSocket; import java.net. socket; import java. util. random; import com. boxun. util. getDate; /*** receive file service ** @ author admin_Hzw **/public class BxServerSocket {/*** Project main method * @ param args */public static void main (String [] args) {try {fin Al ServerSocket server = new ServerSocket (48123); Thread th = new Thread (new Runnable () {public void run () {while (true) {try {System. out. println ("Start listening... ");/** if it is not accessed, it will automatically wait */Socket socket = server. accept (); System. out. println ("linked"); receiveFile (socket);} catch (Exception e) {System. out. println ("server exception"); e. printStackTrace () ;}}}); th. run (); // start the thread to run} catch (Exception e) {e. printSt AckTrace () ;}} public void run () {}/ *** Method for receiving files * @ param socket * @ throws IOException */public static void receiveFile (Socket socket) throws IOException {byte [] inputByte = null; int length = 0; DataInputStream dis = null; FileOutputStream fos = null; String filePath = "D:/temp/" + GetDate. getDate () + "SJ" + new Random (). nextInt (10000) + ". zip "; try {dis = new DataInputStream (socket. getInputSt Ream (); File f = new File ("D:/temp"); if (! F. exists () {f. mkdir ();}/** File storage location */fos = new FileOutputStream (new File (filePath); inputByte = new byte [1024]; System. out. println ("start to receive data... "); while (length = dis. read (inputByte, 0, inputByte. length)> 0) {fos. write (inputByte, 0, length); fos. flush ();} System. out. println ("completed receipt:" + filePath);} finally {if (fos! = Null) fos. close (); if (dis! = Null) dis. close (); if (socket! = Null) socket. close () ;}} catch (Exception e) {e. printStackTrace () ;}} time tool class: [java] import java. text. simpleDateFormat; import java. util. date; /*** time tool class ** @ author admin_Hzw **/public class GetDate {/*** time format to millisecond */private static SimpleDateFormat df = new SimpleDateFormat ("yyyyMMddHHmmssSSS "); public static String getDate () {return df. format (new Date ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.