android socket 通信(2)--传文件

接受文件的线程:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Sthread implements Runnable{
	
	ServerSocket server = null;
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			server = new ServerSocket(33456);
            while (true) {
                try {
                   System.out.println("开始监听。。。");
                   Socket socket = server.accept();
                   System.out.println("有链接");
                   receiveFile(socket);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	

}
    public static void receiveFile(Socket socket) throws IOException {
        byte[] inputByte = null;
        int length = 0;
        DataInputStream din = null;
        FileOutputStream fout = null;
        String savePath; // 文件保存路径
        try {
    		// 判断接收文件的文件夹是否存在,若不存在,则创建
        	savePath = "/mnt/sdcard/atest2/";
    		File fileDir = new File(savePath);
    		if (!fileDir.exists()) { // 若不存在
    			fileDir.mkdir();
    		}

            din = new DataInputStream(socket.getInputStream());
           // fout = new FileOutputStream(new File("/sdcard2/DCIM/Camera/"+din.readUTF()));
			if (new File(savePath+din.readUTF()).exists()) { // 若对应文件名的文件已存在,则删除原来的文件
				new File(savePath+din.readUTF()).delete();
			}
            fout = new FileOutputStream(new File(savePath+din.readUTF()));
            inputByte = new byte[1024];
            System.out.println("开始接收数据...");
            while (true) {
                if (din != null) {
                    length = din.read(inputByte, 0, inputByte.length);
                }
                if (length == -1) {
                    break;
                }
                System.out.println(length);
                fout.write(inputByte, 0, length);
                fout.flush();
            }
            System.out.println("接收完成");
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (fout != null)
                fout.close();
            if (din != null)
                din.close();
            if (socket != null)
                socket.close();
        }
    }
}


发送文件的线程:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.util.Log;

public class Cthread implements Runnable{
	 int length = 0;
     byte[] sendByte = null;
     Socket socket = null;
     DataOutputStream dout = null;
     FileInputStream fin = null;
     String pathString = null;
     String IP = null;
 	public String getIP() {
		return IP;
	}
	public void setIP(String iP) {
		IP = iP;
	}
	public String getPathString() {
		return pathString;
	}
	public void setPathString(String pathString) {
		this.pathString = pathString;
	}
     
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("客户端向本机发起连接");
        socket = new Socket();
        try {
			socket.connect(new InetSocketAddress("255.255.255.255", 33456),10 * 1000);
			dout = new DataOutputStream(socket.getOutputStream());
			File file = new File(pathString);
			fin = new FileInputStream(file);
            sendByte = new byte[1024];
            dout.writeUTF(file.getName());
            while((length = fin.read(sendByte, 0, sendByte.length))>0){
                dout.write(sendByte,0,length);
                dout.flush();
            }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        finally{
            if (dout != null)
				try {
					dout.close();
		            if (fin != null)
		                fin.close();
		            if (socket != null)
		                socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }
	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

obession

觉得有用可以打赏咖啡一杯~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值