Android downloads files via SOCKET

  • 2020-12-05 17:22:43
  • OfStack

This article illustrates how Android downloads files via SOCKET. To share for your reference, the details are as follows:

Server-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 = " The path of the file you want to download ";
 public static void main(String[] args) throws IOException{
  FunctionServer server = new FunctionServer();
  server.start();
 }
 public void start() throws IOException{
  ServerSocket ss = new ServerSocket(PORT);
  while(true){
   Socket s = ss.accept();
   new Service(s).start();// Create a thread 
  }
 }
 class Service extends Thread{
  Socket s;
  public Service(Socket s){
   this.s = s;
  }
  public void run(){
   try{
    InputStream in = s.getInputStream();// Get 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 file name is "+str);
      // Gets the file by path and file name 
      File f = new File(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 = " The 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(); //  Clean up 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 the download ");
     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 completed ");
     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();
    }
   }
  }
 }

Basically can be used directly, according to their own needs a little change on OK

I hope this article has been helpful in Android programming.


Related articles: