Android设备到PC,通过Socket连接进行文件传输

时间:2013-04-23 11:21:49

标签: java android sockets ip-address serversocket

我正在通过Java应用程序将文件从Android模拟器传输到PC服务器。我的客户端应用程序上的代码是......

File file = new File("/mnt/sdcard/sample.mp4"); //create file instance
client = new Socket("10.0.2.2", 4444);
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file

fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream); 

bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file

outputStream = client.getOutputStream();

outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();

但是,当我在Android设备上运行它时,应用程序将无法运行。我想我应该用其他值替换“10.0.2.2”(即:我的PC服务器的IP地址)。我应该放在什么价值?

0 个答案:

没有答案