PC服务器端代码:

/*
* PC与<a href="http://lib.csdn.net/base/android" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>客户端实现文件的传送
* PC服务器端
*/
package com.<a href="http://lib.csdn.net/base/android" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>android</a>.test;
import <a href="http://lib.csdn.net/base/java" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>.ServerSocket;
import java<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.net</a>.Socket;
public class TransferFileServer {
private static final int HOST_PORT = ;
private void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(HOST_PORT);
while (true) {
String filePath = "/home/fan/Pictures/1.jpg";
File file = new File(filePath);
System.out.println("文件长度:" + (int) file.length());
s = ss.accept();
log("建立Socket连接");
DataInputStream dis = new DataInputStream(
new BufferedInputStream(s.getInputStream()));
dis.readByte();
DataInputStream fis = new DataInputStream(
new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(file.getName());
dos.flush();
dos.writeLong((long) file.length());
dos.flush();
int bufferSize = ;
byte[] buf = new byte[bufferSize];
while (true) {
int read = ;
if (fis != null) {
read = fis.read(buf);
}
if (read == -) {
break;
}
dos.write(buf,,read);
}
dos.flush();
// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
// 直到socket超时,导致数据不完整。
fis.close();
s.close();
log("文件传输完成");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void log(String msg) {
System.out.println(msg);
}
public static void main(String args[]) {
new TransferFileServer().start();
}
}

ClientSocket:辅助类

/*
* PC与Android文件传输
* Android客户端
*/
package com.android.test;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.Net</a>.Socket;
import java.net.UnknownHostException;
public class ClientSocket {
private String ip;
private int port;
private Socket socket = null;
DataOutputStream out = null;
DataInputStream getMessageStream = null;
public ClientSocket(String ip, int port) {
this.ip = ip;
this.port = port;
}
public void createConnection() {
try {
socket = new Socket(ip, port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (socket != null) {
try {
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} finally {
}
}
public void sendMessage(String sendMessage) {
try {
out = new DataOutputStream(socket.getOutputStream());
if (sendMessage.equals("Windows")) {
out.writeByte(0x1);
out.flush();
return;
}
if (sendMessage.equals("Unix")) {
out.writeByte(0x2);
out.flush();
return;
}
if (sendMessage.equals("<a href="http://lib.csdn.net/base/linux" class='replace_word' title="Linux知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Linux</a>")) {
out.writeByte(0x3);
out.flush();
} else {
out.writeUTF(sendMessage);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (out != null) {
try {
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public DataInputStream getMessageStream() {
try {
getMessageStream = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
// return getMessageStream;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (getMessageStream != null) {
try {
getMessageStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
return getMessageStream;
}
public void shutDownConnection() {
try {
if (out != null) {
out.close();
}
if (getMessageStream != null) {
getMessageStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Android客户端:Acitivyt实现:

package com.android.test;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
private ClientSocket cs = null;
private static final String FILE_PATH = "/mnt/sdcard/";
private String ip = "192.168.1.103";
private int port = ;
private String sendMessage = "<a href="http://lib.csdn.net/base/linux" class='replace_word' title="Linux知识库" target='_blank' style='color:#df3434; font-weight:bold;'>linux</a>";
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.start);
mButton.setOnClickListener(this);
}
private void start() {
if (createConnection()) {
sendMessage();
getMessage();
}
}
private void getMessage() {
if (cs == null)
return;
DataInputStream inputStream = null;
inputStream = cs.getMessageStream();
try {
String savePath = FILE_PATH;
int bufferSize = ;
byte[] buf = new byte[bufferSize];
int passedlen = ;
long len = ; savePath += inputStream.readUTF();
Log.d("AndroidClient","@@@savePath"+savePath);
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(savePath))));
len = inputStream.readLong();
Log.d("AndoridClient","文件的长度为:"+len);
Log.d("AndroidClient","开始接收文件");
while(true) {
int read = ;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -) {
break;
}
Log.d("AndroidClient","文件接收了"+(passedlen*/len)+"%/n");
fileOut.write(buf,,read);
}
Log.d("AndroidClient","@@@文件接收完成"+savePath);
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMessage() {
if (cs == null)
return;
cs.sendMessage(sendMessage);
}
private boolean createConnection() {
cs = new ClientSocket(ip, port);
cs.createConnection();
Log.d("Main", "连接服务器成功:");
return true;
}
public void onClick(View v) {
start();
}
}

Android 用Socket实现PC和手机的文件传输的更多相关文章

  1. python+socket实现网络信息交互及文件传输

    Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket又称"套接字",应用程序通常通过"套接字" ...

  2. android adb命令,向开发手机添加文件

    adb文档 把本地文件发送到调试手机 C:\Users\ajanuw>adb push C:\Users\ajanuw\Music\j.mp3 /storage/emulated/0/Downl ...

  3. 非堵塞socket实现android手机与PC的文件传输

    项目须要是通过WIFI建立手机和PC的通信,然后自己定义一个简单的协议对要传输的文件进行校验,传输的文件是2张3M的图片,要求考虑网络中断情况处理. 我这里採用的是非堵塞socket来实现的,之前查过 ...

  4. Android Socket连接PC出错问题及解决

    最近测试问题:Android 通过Socket链接电脑,ip和端口都是正确的,也在同一网段,可android端就是报异常如下: 解决办法:测试电脑的防火墙可能开着,在控制面板把防火墙打开即可.

  5. Appium 连手机失败Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localhost undefined:4724

    问题:Appium执行,连接手机报下面的错误 Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localho ...

  6. android视频录制、另一部手机实时观看方案

    最近调研android视频录制.另一部手机实时观看,大致有以下几种思路. 1. android手机充当服务器,使用NanoHTTPD充当服务器,另一部手机或者pc通过输入http://手机的ip:80 ...

  7. Android之socket多线程(二)

    使用ServerSocket创建服务器端: public static void main(String[] args) throws IOException { // TODO Auto-gener ...

  8. Nginx配置网站适配PC和手机

    考虑到网站的在多种设备下的兼容性,有很多网站会有手机版和电脑版两个版本.访问同一个网站URL,当服务端识别出用户使用电脑访问,就打开电脑版的页面,用户如果使用手机访问,则会得到手机版的页面. 1.判断 ...

  9. 【转】Nginx区分PC或手机访问不同网站

    原文链接:http://www.nginx.cn/784.html 近几年来,随着手机和pad的普及,越来越多的用户选择使用移动客户端访问网站,而为了获取更好的用户体验,就需要针对不同的设备显示出最合 ...

随机推荐

  1. View 和 ViewGroup 的 hasFocusable

    在 android 中.焦点的获取和事件几乎相同,有一个分发机制.一般来说View 树上上层节点的 ViewGroup 比底层节点的 View 有更高的优先级获取焦点.这体如今 ViewGroup 有 ...

  2. hdu 思维风暴

    点击打开链接 偶然在杭电上看到的题目,数学题.好像是一道六年级奥赛题目,反正我是没有想出来,也知道往那上面想.就是找不到规律啊.学习了网上的方法, 这道题须要求出来多添加的点,就是与之前每条边添加的点 ...

  3. cocos2d-x《农场模拟经营养成》游戏完整源代码

    cocos2d-x农场模拟经营养成游戏完整源代码,cocos2d-x引擎开发,使用JSON交互,支持IOS与 Android,解压后1016MB. 非常强大的游戏源代码         完整游戏源代码 ...

  4. USACO2011 Jan:公司利润

    简要题意: 奶牛开了家公司,已经连续运作了N 天.它们在第i 天获得了Ai元的利润,不过有些天是亏钱的,这种情况下利润就是一个负数.约翰想为它们写个新闻,吹嘘它们的惊人业绩.请你帮助他选出一段连续的日 ...

  5. Spring MVC 待学习---新特性

    Spring3.1新特性 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.   二.Spring2.5引入注解式处理器支持,通过@Controller ...

  6. java command not found

    INSTALLATION PROCEDURE ON RASPBERRY PI The remaining steps should be performed directly on the conso ...

  7. 2008R2域控环境中 应用组策略 实现禁用USB设备使用

    本文介绍如何在Windows Server 2008 AD中禁用客户端USB端口.本文使用的系统:Windows Server 2008 R2 企业版.域功能级别:Windows Server 200 ...

  8. javaScript call与apply学习笔记

    call和apply是借用他人的函数实现自己到功能,具体表现在改变this指向,借用他人方法 而不同的地方是call是把实参按照形参的个数传入,而apply传入的是一个数组(argument) 写一个 ...

  9. mescroll报错

    1.Cannot read property 'insertBefore' of null:说明你的容器id未找到,应确认你的容器id名与你NEW的容器名一致:

  10. 紫书 习题 10-44 UVa 11246 ( 容斥原理)

    把k的倍数的删去(k, 2k, 3k--),但是k^2不应该删去,因为k已经删去,所以不存在某个数乘上k之后为k^2 所以k^2可以留下,然后因为有k^2,所以k^3就是k^2的k倍,所以k^3要删去 ...