传输Android设备之间的文件吗? [英] Transfer a file between Android devices?
问题描述
我在做一个code,我想送一个MP4文件到另一台Android设备。我已经达到了通过无线连接两个机器人从一个简单的周期,从1-20和其他Android设备读取,并显示被发送的数量写。
I'm making a code and I want to send a mp4 file to another Android device. I have reached to connect both Androids via Wifi and write from one a simple for cycle from 1-20 and the other Android device reads and displays the number that is sent.
这是发件人的有趣的部分:
Here it is the interesting part of the "sender":
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, port);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
for (int i = 1; i < 20; i++) {
out.println(i);
i=i++;
和接收器:
serverSocket = new ServerSocket(SERVERPORT);
// listen for incoming clients
Socket client = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()),8*1024);
这个伟大的工程!但我想从一个设备发送文件到另一台,而不是一个int。我怎样才能让这个?????
This works great! But I want to send a file from one device to another instead of an int. How can I make this?????
推荐答案
您需要通过某种数据格式的数据流中包。这样做的一种方式是使用其通常用于在电子邮件发送附件的共同MIME数据格式。
You need to package the data to the stream through a some kind of data format. One way to do this is to use the common MIME data format which is commonly used to send attachment in email.
我已经回答了有关使用这种格式<一个通过套接字发送二进制其他问题href="http://stackoverflow.com/questions/7374481/android-add-filename-to-bytestream/7374554#7374554">the SO问题如下 - 机器人添加文件名以字节流。你可以检查接受答案这个问题。
I have answered other question related to sending binary via socket using this format in the following SO Question - android add filename to bytestream. You could check the accepted answer for that question.
有关您参考,我只是复制了code发送和通过接口从下面这个问题接受。
For your reference, I just copied the code for sending and receiving through socket from that question below.
File f = new File(path);
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
String filename=path.substring(path.lastIndexOf("/")+1);
// create a multipart message
MultipartEntity multipartContent = new MultipartEntity();
// send the file inputstream as data
InputStreamBody isb = new InputStreamBody(new FileInputStream(f), "image/jpeg", filename);
// add key value pair. The key "imageFile" is arbitrary
multipartContent.addPart("imageFile", isb);
multipartContent.writeTo(out);
out.flush();
out.close();
而code读回用下面的 MimeBodyPart 即JavaMail的的一部分。
And the code to read it back below using MimeBodyPart that is part of JavaMail.
MimeMultipart multiPartMessage = new MimeMultipart(new DataSource() {
@Override
public String getContentType() {
// this could be anything need be, this is just my test case and illustration
return "image/jpeg";
}
@Override
public InputStream getInputStream() throws IOException {
// socket is the socket that you get from Socket.accept()
BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream());
return inputStream;
}
@Override
public String getName() {
return "socketDataSource";
}
@Override
public OutputStream getOutputStream() throws IOException {
return socket.getOutputStream();
}
});
// get the first body of the multipart message
BodyPart bodyPart = multiPartMessage.getBodyPart(0);
// get the filename back from the message
String filename = bodyPart.getFileName();
// get the inputstream back
InputStream bodyInputStream = bodyPart.getInputStream();
// do what you need to do here....
这篇关于传输Android设备之间的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!