2012-02-07 90 views
0

堆垛機... 我有一個共享文件夾中的Web服務器...它的地址是:安卓HTTP客戶端下載廣告上傳文件

"http://192.168.1.1:9999/folder/0" 

我用Google搜索了很多資料有關下載文件和需要幫助。 ..

  1. 下載是成功的...我怎麼上傳文件到我的droid? (服務器權限已啓用)...

    case R.id.download: 
        try { 
         java.net.URL u = new java.net.URL("http://192.168.1.1:9999/file/2/01.mp3"); 
          HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
    
          c.setRequestMethod("GET"); 
          c.setDoOutput(true); 
          c.connect(); 
          FileOutputStream f = new FileOutputStream(new File("/sdcard/in", "01.mp3")); 
    
    
          InputStream in = c.getInputStream(); 
    
          byte[] buffer = new byte[1024]; 
          int len1 = 0; 
          while ((len1 = in.read(buffer)) > 0) 
          { 
           f.write(buffer,0, len1); 
          } 
          f.close(); 
        } 
        catch (Exception e) { 
         System.out.println("Nay, did not work"); 
         textView.setText(e.getMessage()); 
        } 
        break; 
    
  2. 網絡連接失敗,尤其是對於移動客戶端而言,頻繁出現故障。例如,如果您從WiFi切換到3G,則現有網絡連接將斷開,您需要重試請求。 Apache HttpClient有一個默認的DefaultHttpRequestRetryHandler對象註冊,默認3次重試失敗的連接。問題是從一個網絡切換到另一個需要一點時間,並且DefaultHttpRequestRetryHandler將立即重試。如何實現它我的羽絨/ UpLoading?

回答

1
Please make sure you have the required Web Services for Posting. 
Just pass the file and url of server and the following should run. 

    try{ 
        int maxBufferSize=1024*1024; 
        File file = new File(YourPathToFile); 
        String fileName = file.getName(); 

         URL url = new URL(YourUrlServer); 
         connection = (HttpURLConnection) url.openConnection(); 
        // Allow Inputs & Outputs 
        connection.setDoInput(true); 
        connection.setDoOutput(true); 
        connection.setUseCaches(false); 
        // Enable POST method 
        connection.setRequestMethod("GET"); 
        connection.setRequestProperty("Connection", "Keep-Alive"); 
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
        connection.setChunkedStreamingMode(maxBufferSize); 
        outputStream = new DataOutputStream(connection.getOutputStream()); 

        fileInputStream = new FileInputStream(file); 


        outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
        outputStream.writeBytes("Content-Disposition: form-data; name=\"strAuthKey\"" + lineEnd); 
        outputStream.writeBytes(lineEnd); 
        outputStream.writeBytes(SoapRequestProcessor.authKey());//authentication key 
        outputStream.writeBytes(lineEnd); 

        outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
        outputStream.writeBytes("Content-Disposition: form-data; name=\"mediaName\"" + lineEnd); 
        outputStream.writeBytes(lineEnd); 
        outputStream.writeBytes(fileName); //file.lastModified()+"_"+ 
        outputStream.writeBytes(lineEnd); 


        outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"IMAGEFILE\"" + lineEnd); 
        outputStream.writeBytes(lineEnd); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
        while (bytesRead > 0) 
        { 
        outputStream.write(buffer, 0, bufferSize); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
        } 

        outputStream.writeBytes(lineEnd); 



        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
        fileInputStream.close(); 
         outputStream.close(); 
        Log.d("RESPONSE","--"+connection.getResponseMessage()); 
    } 
        catch (Exception e) { 
        Log.i("Exception: ",e.toString()); 

        // TODO: handle exception 
       } 
+0

SoapRequestProcessor.authKey() - 它是什麼? – timonvlad 2012-02-07 10:01:20

+0

您可以跳過該行。實際上在我的情況下,身份驗證密鑰也是需要的。 – SAMD 2012-02-07 12:32:26

2

要上傳您可以使用多部分請求的文件: http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html

 client = new DefaultHttpClient(...); 
     HttpPost post = new HttpPost("some url"); 
     MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     multipart.addPart("Description", new StringBody("Description")); 
     multipart.addPart("File", new FileBody(fileObject)); 
     post.setEntity(multipart); 

     client.execute(post, new ResponseHandler() { 

     @Override 
     public Object handleResponse(HttpResponse response) 
      throws ClientProtocolException, IOException { 
      //handle response 
      } 
     });