2011-10-04 56 views
5

我正在做一個代碼,我想發送一個mp4文件到另一個Android設備。我已經通過Wifi連接兩個Android,並從一個簡單的1-20週期寫入,另一個Android設備讀取並顯示發送的號碼。在Android設備之間傳輸文件?

這是 「發件人」 的有趣的部分:

   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。我該如何做到這一點?

回答

1

您需要通過某種數據格式將數據打包到流中。一種方法是使用通常用於在電子郵件中發送附件的通用MIME數據格式。

我已經回答了在the following SO Question - android add filename to bytestream中使用此格式通過套接字發送二進制文件的其他相關問題。您可以查看該問題的接受答案。

爲了供您參考,我剛剛從以下問題中複製了通過套接字發送和接收的代碼。

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(); 

並且使用屬於JavaMail的MimeBodyPart來回讀以下代碼。


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.... 
+0

不錯!所以我會忽略文件名的一部分。 什麼是「multipartContent.addPart(」imageFile「,isb); 」??? 而我怎樣才能得到另一個Android手機中的文件?因爲你指定的答案是爲JavaMail? 謝謝! – Merol

+0

由於JavaMail包含MIME格式的庫(MimeMultipart),因此只使用JavaMail,但它與郵件無關。文件名基本上是您傳輸到另一端的文件名的元數據(如果沒關係,您可以放入任何內容),「imageFile」只是數據的關鍵字,您可以將其命名爲任何內容。這只是作爲一個樣本。如果我需要在我的項目中傳輸二進制文件,我使用相同的模式,因爲這證明可以在電子郵件附件中使用。這比設計我自己的數據格式更簡單 – momo

0

有發佈了一個開源項目由谷歌你可以看看它,並有關於連接設備以及它們之間共享文件的一般概念。

這裏是鏈接:android-fileshare

相關問題