基本信息
源码名称:将PC电脑端的文件通过usb传到android手机中 示例源码下载(含android和pc端完整源码)
源码大小:2.05M
文件格式:.rar
开发语言:Java
更新时间:2014-06-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.chenhl.testusbconnect;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.content.Context;
import android.util.Log;
import com.chenhl.testusbconnect.utils.FileHelper;
import com.chenhl.testusbconnect.utils.MyUtil;
public class ThreadReadWriterIOSocket implements Runnable
{
private Socket client;
private Context context;
public ThreadReadWriterIOSocket(Context context, Socket client)
{
this.client = client;
this.context = context;
}
@Override
public void run()
{
Log.d("chl", "a client has connected to server!");
BufferedOutputStream out;
BufferedInputStream in;
try
{
/* PC端发来的数据msg */
String currCMD = "";
out = new BufferedOutputStream(client.getOutputStream());
in = new BufferedInputStream(client.getInputStream());
androidService.ioThreadFlag = true;
while (androidService.ioThreadFlag)
{
try
{
if (!client.isConnected())
{
break;
}
/* 接收PC发来的数据 */
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "will read......");
/* 读操作命令 */
currCMD = readCMDFromSocket(in);
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "**currCMD ==== " currCMD);
/* 根据命令分别处理数据 */
if (currCMD.equals("1"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("2"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("3"))
{
out.write("OK".getBytes());
out.flush();
} else if (currCMD.equals("4"))
{
/* 准备接收文件数据 */
try
{
out.write("service receive OK".getBytes());
out.flush();
} catch (IOException e)
{
e.printStackTrace();
}
/* 接收文件数据,4字节文件长度,4字节文件格式,其后是文件数据 */
byte[] filelength = new byte[4];
byte[] fileformat = new byte[4];
byte[] filebytes = null;
/* 从socket流中读取完整文件数据 */
filebytes = receiveFileFromSocket(in, out, filelength, fileformat);
// Log.v(Service139.TAG, "receive data =" new
// String(filebytes));
try
{
/* 生成文件 */
File file = FileHelper.newFile("ucliulanqi.apk");
FileHelper.writeFile(file, filebytes, 0, filebytes.length);
} catch (IOException e)
{
e.printStackTrace();
}
} else if (currCMD.equalsIgnoreCase("exit"))
{
out.write("exit ok".getBytes());
out.flush();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
out.close();
in.close();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (client != null)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "client.close()");
client.close();
}
} catch (IOException e)
{
Log.e(androidService.TAG, Thread.currentThread().getName() "---->" "read write error333333");
e.printStackTrace();
}
}
}
/**
* 功能:从socket流中读取完整文件数据
*
* InputStream in:socket输入流
*
* byte[] filelength: 流的前4个字节存储要转送的文件的字节数
*
* byte[] fileformat:流的前5-8字节存储要转送的文件的格式(如.apk)
*
* */
public static byte[] receiveFileFromSocket(InputStream in, OutputStream out, byte[] filelength, byte[] fileformat)
{
byte[] filebytes = null;// 文件数据
try
{
in.read(filelength);// 读文件长度
int filelen = MyUtil.bytesToInt(filelength);// 文件长度从4字节byte[]转成Int
String strtmp = "read file length ok:" filelen;
out.write(strtmp.getBytes("utf-8"));
out.flush();
filebytes = new byte[filelen];
int pos = 0;
int rcvLen = 0;
while ((rcvLen = in.read(filebytes, pos, filelen - pos)) > 0)
{
pos = rcvLen;
}
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "read file OK:file size="
filebytes.length);
out.write("read file ok".getBytes("utf-8"));
out.flush();
} catch (Exception e)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "receiveFileFromSocket error");
e.printStackTrace();
}
return filebytes;
}
/* 读取命令 */
public String readCMDFromSocket(InputStream in)
{
int MAX_BUFFER_BYTES = 2048;
String msg = "";
byte[] tempbuffer = new byte[MAX_BUFFER_BYTES];
try
{
int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");
tempbuffer = null;
} catch (Exception e)
{
Log.v(androidService.TAG, Thread.currentThread().getName() "---->" "readFromSocket error");
e.printStackTrace();
}
// Log.v(Service139.TAG, "msg=" msg);
return msg;
}
}