2012-03-08 258 views
2

我想從客戶端上傳文件到服務器。如何處理從Java客戶端上傳的服務器端HTTP文件

客戶端:Java中使用HTTP發佈 服務器:Java Servlet的

我已經添加了客戶端在這裏編碼。但是,我不知道服務器端處理。請用代碼片段幫助我。

private String Tag = "UPLOADER"; 
    private String urlString = "http://192.168.42.140:8080/sampweb"; 
    HttpURLConnection conn; 
    String exsistingFileName = "/sdcard/log.txt"; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    try { 
     // ------------------ CLIENT REQUEST 

     Log.e(Tag, "Inside second Method"); 

     FileInputStream fileInputStream = new FileInputStream(new File(
       exsistingFileName)); 

     // open a URL connection to the Servlet 

     URL url = new URL(urlString); 

     // Open a HTTP connection to the URL 

     conn = (HttpURLConnection) url.openConnection(); 

     // Allow Inputs 
     conn.setDoInput(true); 

     // Allow Outputs 
     conn.setDoOutput(true); 

     // Don't use a cached copy. 
     conn.setUseCaches(false); 

     // Use a post method. 
     conn.setRequestMethod("POST"); 

     conn.setRequestProperty("Connection", "Keep-Alive"); 

     conn.setRequestProperty("Content-Type", 
       "multipart/form-data;boundary=" + boundary); 

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

     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos 
       .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename=" 
         + exsistingFileName + "" + lineEnd); 
     dos.writeBytes(lineEnd); 

     Log.e(Tag, "Headers are written"); 

     // create a buffer of maximum size 

     int bytesAvailable = fileInputStream.available(); 
     int maxBufferSize = 1000; 
     // int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     byte[] buffer = new byte[bytesAvailable]; 

     // read file and write it into form... 

     int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); 

     while (bytesRead > 0) { 
      dos.write(buffer, 0, bytesAvailable); 
      bytesAvailable = fileInputStream.available(); 
      bytesAvailable = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); 
     } 

     // send multipart form data necessary after file data... 

     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

     // close streams 
     Log.e(Tag, "File is written"); 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 

    } catch (MalformedURLException ex) { 
     Log.e(Tag, "error: " + ex.getMessage(), ex); 
    } 

    catch (IOException ioe) { 
     Log.e(Tag, "error: " + ioe.getMessage(), ioe); 
    } 

    try { 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn 
       .getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      Log.e("Dialoge Box", "Message: " + line); 
     } 
     rd.close(); 

    } catch (IOException ioex) { 
     Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
    } 
} 

我是新來的服務器編程。如果我犯了什麼錯誤,請澄清我。謝謝!

+0

標題有誤導性,但問題本身在試圖在Java中實現* server *端而不是在客戶端上很明顯。當前鏈接的副本描述客戶端實現。 – 2016-02-03 15:06:02

回答

-1

達到此目的的最佳圖書館仍然是Apache Commons File Upload。它是有據可查的,易於使用。如果遇到任何問題,請首先檢查FAQ

+1

這不起作用。 Commons File Upload用於服務器而不是客戶端。 – 2015-08-23 20:11:02

+0

問題是:*「我在這裏添加了客戶端編碼,但是,我對服務器端處理沒有任何想法。」* – 2015-08-24 04:41:23

+0

@GergelyBasco,如果您閱讀標題,則問題還不清楚。真正的問題已經減少到一句話。感謝指針。 – 2015-08-24 07:13:27

相關問題