How Android downloads files via the socket _android

Source: Internet
Author: User
Tags flush

This example describes how Android downloads files via the socket. Share to everyone for your reference, specific as follows:

Service-side code

Import Java.io.BufferedInputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Java.util.Scanner;
 public class Functionserver {private static int PORT = 2012;
 Private String Path = "path where files need to be downloaded";
  public static void Main (string[] args) throws ioexception{functionserver server = new Functionserver ();
 Server.start ();
  The public void start () throws ioexception{ServerSocket ss = new ServerSocket (PORT);
   while (true) {Socket s = ss.accept ();
  New service (s). Start ()///Create Thread} class Service extends thread{Socket s;
  The public Service (Socket s) {this.s = s;
    The public void Run () {try{InputStream in = S.getinputstream ();//Gets the input stream Scanner sc = new Scanner (in);
    OutputStream out = S.getoutputstream (); while (true) {String str = sc.Nextline ()///Read file name if (!str.equals (null)) {SYSTEM.OUT.PRINTLN ("Your filename is" +str);
      Gets the file, based on the path and file name (PATH+STR);
      FileInputStream fis = new FileInputStream (f);
      DataInputStream dis = new DataInputStream (new Bufferedinputstream (FIS));
      byte[] buffer = new byte[8192];
      DataOutputStream PS = new DataOutputStream (out);
      Ps.writelong ((Long) f.length ());//Send File size Ps.flush ();
       while (true) {int read = 0;
       if (dis!=null) {read = Fis.read (buffer);
       } if (read = = 1) {break;
      } ps.write (Buffer,0,read);
      } ps.flush ();
      Dis.close ();
      S.close ();
      Out.flush ();
     Break
   }}catch (IOException e) {e.printstacktrace ();

 }
  }
 }
}

Client code, download thread

Class Downloadthread extends Thread {socket socket;
  InputStream in;
  OutputStream out;
  String Path = "File save path";
  String functionname;
  String ServerIP = "Server IP";
  int socketport = "service port number";
  int filesize,downloadfilesize;
  Public Downloadthread (String functionname) {this.functionname = functionname;
   @Override public void Run () {looper.prepare (); while (!
     Thread.interrupted ()) {try {socket = new socket (ServerIP, socketport);
     InputStream in = Socket.getinputstream ();
     OutputStream out = Socket.getoutputstream ();
     Out.write ((functionname + "\ n"). GetBytes ("GBK")); Out.flush ();
     Clears the buffer to ensure that it is sent to the server file F = new file (path + functionname);
     OutputStream song = new FileOutputStream (f);
     DataInputStream dis = new DataInputStream (new Bufferedinputstream (in));
     DataOutputStream dos = new DataOutputStream (new Bufferedoutputstream (song));
     fileSize = (int) Dis.readlong ()-1;
 System.out.println ("Start downloading");    byte[] buffer = new byte[8192];
      while (true) {int read = 0;
       if (dis!= null) {read = Dis.read (buffer);
        Downloadfilesize + = read;
      } if (read = = 1) {break;
     } dos.write (buffer, 0, read);
     } System.out.println ("File download Complete");
    Dos.close ();
    catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
    finally {this.interrupt ();

 }
   }
  }
 }

Basic can be used directly, according to their need to change a little bit OK

I hope this article will help you with the Android program.

Related Article

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.