android socket 通信(2)--传文件
来源:互联网 发布:北大图书馆知乎 编辑:程序博客网 时间:2024/06/06 13:05
接受文件的线程:
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;@Overridepublic void run() {// TODO Auto-generated method stubtry {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 blocke.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;} @Overridepublic void run() {// TODO Auto-generated method stubSystem.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 blocke.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 blocke.printStackTrace();} }}}
0 0
- android socket 通信(2)--传文件
- android socket通信(上)
- android socket通信(下)
- android socket通信(上)
- android socket通信(下)
- android socket通信(上)
- android socket通信(下)
- vb socket通信 大文件端口上传(2)
- 基于TCP的Socket通信传文件
- 基于TCP的Socket通信传文件
- Android通信一:Socket通信
- Android 使用socket通信来实现文件断点续传上传
- Android中的socket通信(四)
- Android USB 通信(Socket 详解)
- Android Socket网络通信
- Android Socket网络通信
- android socket通信
- Android Socket网络通信
- java数据结构之(顺序栈+链式栈)
- <LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree
- NodeJs——(1)封装,调用,执行,访问路径,http,函数编程,等待函数,事件监听
- sql技巧-构造分组条件
- 【黑马Android】(13)Linux操作系统/cron计划任务
- android socket 通信(2)--传文件
- JAVA 数据类型
- Oracle11gR2_RAC_for_redhat6.3 安装手册
- POJ 2155 Matrix
- GitHub网站文件删除
- Android 基础总结:( 十三)AsyncTask
- Java接入Spark之创建RDD的两种方式和操作RDD
- 1055. 集体照 (25)
- J - 卿大爷的两个女友(待续)