热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android通过SOCKET下载文件的方法

这篇文章主要介绍了Android通过SOCKET下载文件的方法,实例分析了Android使用Socket进行文件传输的技巧,需要的朋友可以参考下

本文实例讲述了Android通过SOCKET下载文件的方法。分享给大家供大家参考,具体如下:

服务端代码

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 = "需要下载的文件所在路径";
 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();//创建线程
  }
 }
 class Service extends Thread{
  Socket s;
  public Service(Socket s){
   this.s = s;
  }
  public void run(){
   try{
    InputStream in = s.getInputStream();//得到输入流
    Scanner sc = new Scanner(in);
    OutputStream out = s.getOutputStream();
    while(true){
     String str = sc.nextLine();//读取文件名
     if(!str.equals(null)){
      System.out.println("你的文件名是"+str);
      //根据路径和文件名获取文件
      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());//发送文件大小
      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();
   }
  }
 }
}

客户端代码,下载线程

class DownloadThread extends Thread {
  Socket socket;
  InputStream in;
  OutputStream out;
  String path = "文件保存路径";
  String functionName;
  String serverIp = "服务器IP";
  int socketPort = "服务端口号";
  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(); // 清理缓冲,确保发送到服务端
     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("开始下载");
     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("文件下载完成");
     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();
    }
   }
  }
 }

基本可以直接用,根据自己需要稍微改动就OK了

希望本文所述对大家Android程序设计有所帮助。


推荐阅读
author-avatar
kaga1990_106
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有