2011-04-26 72 views
5

我上傳圖片到服務器的代碼是:Android:如何將.mp3文件和圖像上傳到http服務器?

String userIdParameter = String.valueOf(userId); 
    String fileName = "temporary_holder.jpg"; 
    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 

    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 

    String sourceFileUri = HomeScreen.get_path(); 
    String upLoadServerUri = "http://10.120.10.87:8080/WebImage/UploadImage"; 

    File sourceFile = new File(sourceFileUri); 
    if (!sourceFile.isFile()) { 
     Log.e("Huzza", "Source File Does not exist"); 
     return; 
    } 
    int serverResponseCode = 0; 
    try { 

     // open a URL connection to the Servlet 
     FileInputStream fileInputStream = new FileInputStream(sourceFile); 

     // ------------------ CLIENT REQUEST 
     URL url = new URL(upLoadServerUri); 

     // Open a HTTP connection to the URL 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoInput(true); // Allow Inputs 
     conn.setDoOutput(true); // Allow Outputs 
     conn.setUseCaches(false); // Don't use a Cached Copy 

     // Use a post method. 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 

     conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
     conn.setRequestProperty("Content-Type", 
      "multipart/form-data;boundary=" + boundary); 
     conn.setRequestProperty("file_name", fileName); 
     conn.setRequestProperty("file_name_audio", fileName); 
     conn.setRequestProperty("X-myapp-param1", userIdParameter); 

     // conn.setFixedLengthStreamingMode(1024); 
     // conn.setChunkedStreamingMode(1); 

     dos = new DataOutputStream(conn.getOutputStream()); 

     dos.writeBytes(twoHyphens + boundary + lineEnd); 

     dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\"" 
      + fileName + "\"" + lineEnd); 

     dos.writeBytes(lineEnd); 


      // create a buffer of maximum size 
     bytesAvailable = fileInputStream.available(); 

     int streamSize = (int) sourceFile.length(); 
     bufferSize = streamSize/10; 

     System.out.println("streamSize" + streamSize); 

     buffer = new byte[streamSize]; 

     // read file and write it into form... 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     int count = 0; 
     while (bytesRead > 0) { 
     progress = (int) (count); 
     displayNotification(); 
     Thread.sleep(500); 

     dos.write(buffer, 0, bufferSize); 
     bytesAvailable = fileInputStream.available(); 
     // bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     count += 10; 

     } 

     // send multipart form data necesssary after file data... 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

     // Responses from the server (code and message) 
     serverResponseCode = conn.getResponseCode(); 
     String serverResponseMessage = conn.getResponseMessage(); 

     System.out.println("Upload file to serverHTTP Response is : " 
      + serverResponseMessage + ": " + serverResponseCode); 
     // close streams 
     System.out.println("Upload file to server" + fileName 
      + " File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 
    } catch (MalformedURLException ex) { 
     ex.printStackTrace(); 
     Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    // this block will give the response of upload link 
    try { 
     BufferedReader rd = new BufferedReader(new InputStreamReader(
      conn.getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
     System.out.println("RESULT Message: " + line); 
     } 
     rd.close(); 
    } catch (IOException ioex) { 
     Log.e("Huzza", "error: " + ioex.getMessage(), ioex); 
    } 
    return; // like 200 (Ok) 

圖片上傳到服務器工作正常。我需要同時擁有MP3文件和圖片上傳到server..Please幫助

+0

嗨詹妮弗,我有一個大問題。你是如何通過將要上傳的圖像參數的? – amity 2011-08-10 12:41:58

+0

檢查 [這](http://stackoverflow.com/questions/4966910/androidhow-to-upload-mp3-file-to-http-server) – Grook 2011-04-26 04:39:33

+0

這種方式是上傳一個mp3文件..我需要上傳兩個MP3和一個圖像..上傳2個文件..我想知道如何準備上傳2個文件的形式 – jennifer 2011-04-26 04:42:30

回答

4

所以,你要在一個HTTP請求中發送多個文件?我從來沒有這個工作我自己,而是根據RFC,只需添加另一個身體到您發送音頻消息,它應該是這個樣子:

dos = new DataOutputStream(conn.getOutputStream()); 
    dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\"" 
     + fileName + "\"" + lineEnd); 
    dos.writeBytes(lineEnd); 
    // Code for sending the image.... 
    dos.writeBytes(lineEnd); 


    dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name=\"file_name_audio\";filename=\"" 
     + fileNameAudio + "\"" + lineEnd); 
    dos.writeBytes(lineEnd); 
    // Code for sending the MP3 
    dos.writeBytes(lineEnd); 
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

確保兩個部件的名稱是不同的(取決於服務器軟件)。

+0

@stephan:如果我想傳遞字符串值與這個多部分像視頻和音頻文件與用戶名及其名稱爲「user_name」和「user_id」的ID。而這個user_id和user_name是字符串。 – 2013-03-06 08:23:50

相關問題