Android network programming only LAN transfer files

Source: Internet
Author: User
Tags getmessage sendfile

Android network programming LAN transfer files:

First, create a socket management class, which is the core class for transferring files, primarily for sending files and receiving files

The specific code is as follows:

1  PackageCom.jiao.filesend;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 ImportJava.io.InputStream;8 ImportJava.io.InputStreamReader;9 ImportJava.io.OutputStream;Ten ImportJava.io.OutputStreamWriter; One ImportJava.net.ServerSocket; A ImportJava.net.Socket; -  - Importandroid.os.Environment; the  - /** - * Socket Management class - * 1. Receiving Documents + * 2. Send file -  * @authorJIAOCG +  * A  */ at  Public classSocketmanager { -     Privateserversocket server; -      PublicSocketmanager (serversocket server) { -          This. Server =server; -     } -     //Receiving Files in      PublicString Receivefile () { -         Try{ to             //Receive file name +Socket name =server.accept (); -             //gets the input stream of the data theInputStream Namestream =Name.getinputstream (); *             //convert a character stream to a byte flow $InputStreamReader StreamReader =NewInputStreamReader (namestream);Panax NotoginsengBufferedReader br =NewBufferedReader (streamReader); -             //Receive file name theString FileName =br.readline (); + br.close (); A streamreader.close (); the namestream.close (); + name.close (); -             //receiving file Data $Socket data =server.accept (); $InputStream DataStream =Data.getinputstream (); -String Savepath = Environment.getexternalstoragedirectory (). GetPath () + "/" +FileName; -FileOutputStream file =NewFileOutputStream (Savepath,false); the             byte[] buffer =New byte[1024]; -             intsize =-1;Wuyi              while(size = datastream.read (buffer))! = 1){ theFile.write (buffer, 0, size); -             } Wu file.close (); - datastream.close (); About data.close (); $             returnFileName + "---receive complete" + "---save to:" +Savepath; -}Catch(Exception e) { - System.out.println (e.tostring ()); -             return"Receive error: \ n" +e.getmessage (); A         } +     } the      -     //Send File $      PublicString SendFile (String fileName, String path, String ipAddress,intPort) { the         Try { the             //Send file name the             //First send file name theSocket name =NewSocket (ipAddress, port); -             //Create an output stream inOutputStream Outputname =Name.getoutputstream (); the             //convert a character stream to a byte flow theOutputStreamWriter Outputwriter =NewOutputStreamWriter (outputname); About             //buffers theBufferedWriter Bwname =NewBufferedWriter (outputwriter); the             //Write to Buffer the Bwname.write (fileName); + bwname.close (); - outputwriter.close (); the outputname.close ();Bayi name.close (); the              the             //Send file Data -Socket data =NewSocket (ipAddress, port); -             //Create an output stream theOutputStream Outputdata =Data.getoutputstream (); the             //get input bytes from a file in the file system theFileInputStream FileInput =NewFileInputStream (path); the             intsize =-1; -             byte[] buffer =New byte[1024]; the              while(size = fileinput.read (buffer, 0, 1024))! =-1){ theOutputdata.write (buffer, 0, size); the             }94 outputdata.close (); the fileinput.close (); the data.close (); the             returnFileName + "Send Complete";98}Catch(Exception e) { About             return"Send error: \ n" +e.getmessage (); -         } 101     }102}

We can use the above file transfer class to implement the file transfer in the LAN

The following code is used:

1 // 1. FileName 2, File path 3, Destination IP 4, destination port 2 String response = Socketmanager.sendfile (fileName, Filepath,ip, POST); 3 message.obtain (handler, 0, Response). Sendtotarget ();

Call the above code can realize the file transfer in the LAN;

Note: The above file transfer belongs to the network transmission, remember in the sub-thread execution Oh, otherwise to error!

Android network programming only LAN transfer files

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.