Socket文件传输

2017-07-06  本文已影响0人  phi3

服务器端(字节流):

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Controled {
public static void main(String[] args) throws IOException {
int length=0;
byte[] sendBytes = null;
Socket s = null;
FileInputStream fis = null;
ServerSocket ss;
InetAddress string=null;
try{
ss=new ServerSocket(45);//建立serversocket端口
System.out.println("server on!");
s=ss.accept();//等待客户端
string=s.getInetAddress();
System.out.println(string+" link success");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
fis = new FileInputStream("D://a.txt");//要发送的文件路径
sendBytes=new byte[1024];定义字节数组
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0){
dos.write(sendBytes, 0, length);写到客户端去
dos.flush();
}
System.out.println("发送成功");
}catch(Exception e){
e.printStackTrace();
}
}
}

客户端:

与之前的socket字符串相同(主要不同部分):
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"run:1111111111111");
accept=new Accept(handler);
new Thread(accept).start();
}
});
handler=new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what==0)
textView.append("接收开始");
}
};

Client.class:

public class Accept implements Runnable{
private Socket s;
byte[] inputByte = null;
int length = 0;
DataInputStream dis = null;
FileOutputStream fos = null;
public Handler revhandler;

public Accept(Handler handler) {
    revhandler=handler;
}

@Override
public void run() {
      try{
          s=new Socket("192.168.0.104",45);//与服务器建立连接
          Log.d(TAG, "run:2222222222222");
           InputStream is = s.getInputStream();
          dis = new DataInputStream(s.getInputStream());
          Log.d(TAG,"run:333333333333333333333");
          inputByte = new byte[1024];
          new Thread(){//创建线程
              @Override
              public void run(){
                  super.run();
                 try{
                     Message msg=new Message();
                     msg.what=0;
                     revhandler.sendMessage(msg);//更新ui
                     Log.d(TAG,"run:44444444444444");
                     while ((length = dis.read(inputByte, 0, inputByte.length)) > 0){
                       File file= new File(手机的内部绝对路径);
                         Log.d(TAG,"444444444444");
                         if(file!=null) {
                             Log.d(TAG, "run:555555555555555");
                             fos = new FileOutputStream(file);//写入所指定的路径文件
                             fos.write(inputByte, 0, length);
                             fos.flush();
                         }
                     }
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
              }
          }.start();
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
}

添加权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

上一篇 下一篇

猜你喜欢

热点阅读