Android网络编程只局域网传输文件

Android网络编程之局域网传输文件:

首先创建一个socket管理类,该类是传输文件的核心类,主要用来发送文件和接收文件

具体代码如下:

  1 package com.jiao.filesend;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.InputStream;
  8 import java.io.InputStreamReader;
  9 import java.io.OutputStream;
 10 import java.io.OutputStreamWriter;
 11 import java.net.ServerSocket;
 12 import java.net.Socket;
 13 
 14 import android.os.Environment;
 15 
 16 /**
 17  * Socket管理类
 18  * 1、接收文件
 19  * 2、发送文件
 20  * @author jiaocg
 21  *
 22  */
 23 public class SocketManager {
 24     private ServerSocket server;
 25     public SocketManager(ServerSocket server){
 26         this.server = server;
 27     }
 28     //接收文件
 29     public String ReceiveFile(){
 30         try{
 31             //接收文件名
 32             Socket name = server.accept();
 33             //获取数据的输入流
 34             InputStream nameStream = name.getInputStream();
 35             //字符流转换为字节流
 36             InputStreamReader streamReader = new InputStreamReader(nameStream);
 37             BufferedReader br = new BufferedReader(streamReader);
 38             //接收到文件名
 39             String fileName = br.readLine();
 40             br.close();
 41             streamReader.close();
 42             nameStream.close();
 43             name.close();
 44             //接收文件数据
 45             Socket data = server.accept();
 46             InputStream dataStream = data.getInputStream();
 47             String savePath = Environment.getExternalStorageDirectory().getPath() + "/" + fileName;
 48             FileOutputStream file = new FileOutputStream(savePath, false);
 49             byte[] buffer = new byte[1024];
 50             int size = -1;
 51             while ((size = dataStream.read(buffer)) != -1){
 52                 file.write(buffer, 0 ,size);
 53             }
 54             file.close();
 55             dataStream.close();
 56             data.close();
 57             return fileName + "---接收完成"+"---保存至:"+savePath;
 58         }catch(Exception e){
 59             System.out.println(e.toString());
 60             return "接收错误:\n" + e.getMessage();
 61         }
 62     }
 63     
 64     //发送文件
 65     public String SendFile(String fileName, String path, String ipAddress, int port){
 66         try {
 67             //发送文件名
 68             //首先发送文件名
 69             Socket name = new Socket(ipAddress, port);
 70             //创建输出流
 71             OutputStream outputName = name.getOutputStream();
 72             //字符流转换为字节流
 73             OutputStreamWriter outputWriter = new OutputStreamWriter(outputName);
 74             //缓冲区
 75             BufferedWriter bwName = new BufferedWriter(outputWriter);
 76             //写到缓冲区
 77             bwName.write(fileName);
 78             bwName.close();
 79             outputWriter.close();
 80             outputName.close();
 81             name.close();
 82             
 83             //发送文件数据
 84             Socket data = new Socket(ipAddress, port);
 85             //创建输出流
 86             OutputStream outputData = data.getOutputStream();
 87             //从文件系统中的某个文件中获得输入字节
 88             FileInputStream fileInput = new FileInputStream(path);
 89             int size = -1;
 90             byte[] buffer = new byte[1024];
 91             while((size = fileInput.read(buffer, 0, 1024)) != -1){
 92                 outputData.write(buffer, 0, size);
 93             }
 94             outputData.close();
 95             fileInput.close();
 96             data.close();
 97             return fileName + " 发送完成";
 98         } catch (Exception e) {
 99             return "发送错误:\n" + e.getMessage();
100         } 
101     }
102 }

 

我们可以利用上述的文件传输类,来实现局域网中的文件传输

具体使用代码如下:

1 // 1、文件名 2、文件路径 3、目标ip 4、目标端口
2 String response = socketManager.SendFile(fileName, filePath,ip, POST);
3 Message.obtain(handler, 0, response).sendToTarget();

 

调用以上代码就可以实现局域网中的文件传输;

注意:以上文件传输属于网络传送,记得在子线程中执行哦,否则要报错!

你可能感兴趣的:(Android网络编程只局域网传输文件)