2012-07-21 53 views
1
I would like to upload a name.txt file without ussing MultiEntitiy "importing a large file" as my file is small in size... I have written the following code but unable to upload a file.. thanks for your concern... 

URL url = new URL(strUrl); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setDoInput(true); // Allow Inputs 
    conn.setDoOutput(true); // Allow Outputs 
    conn.setUseCaches(false); // Don't use a Cached Copy 
    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("userfile", strEmailAddress + ".txt"); 
    dos = new DataOutputStream(conn.getOutputStream()); 
    dos.writeBytes("Content-Disposition: form-data; name=\"paramName\""); 
    Helper.printLogD(" connection " + conn.getContent().toString()); 

String temp = getStringFromInputStream(conn.getInputStream()); 
Helper.printLogI("temp" + temp); 
+1

請換個問題。你*忘了*添加問題 – adatapost 2012-07-21 07:45:46

+0

我認爲你應該在上傳文件或圖像時添加邊界,但如果你正在獲得更好的粘貼問題和錯誤。 – Anand 2012-07-21 08:14:14

+0

已經不存在了,當問問題意味着提問時:-) – 2012-07-21 08:48:37

回答

1

我使用下面的代碼:

public static JSONObject uploadImageToServer(String url, String path, 
     String usuario) { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpPost httpPost = new HttpPost(url); 

    try { 
     MultipartEntity entity = new MultipartEntity(
       HttpMultipartMode.BROWSER_COMPATIBLE); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("userfile", path)); 
     nameValuePairs.add(new BasicNameValuePair("tmp_name", usuario)); 

     for (int index = 0; index < nameValuePairs.size(); index++) { 
      if (nameValuePairs.get(index).getName() 
        .equalsIgnoreCase("userfile")) { 
       // If the key equals to "image", we use FileBody to transfer 
       // the data 
       entity.addPart(nameValuePairs.get(index).getName(), 
         new FileBody(new File(nameValuePairs.get(index) 
           .getValue()))); 
      } else { 
       // Normal string data 
       entity.addPart(
         nameValuePairs.get(index).getName(), 
         new StringBody(nameValuePairs.get(index).getValue())); 
      } 
     } 

     httpPost.setEntity(entity); 

     HttpResponse response = httpClient.execute(httpPost, localContext); 

     HttpEntity responseEntity = response.getEntity(); 

     if (responseEntity != null) { 
      InputStream instream = responseEntity.getContent(); 
      String resultString = convertStreamToString(instream); 
      instream.close(); 
      // Transform the String into a JSONObject 
      JSONObject jsonObjRecv = null; 
      try { 
       jsonObjRecv = new JSONObject(resultString); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return jsonObjRecv; 
     } 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

您還需要圖書館httpmime,使其工作。

希望能幫到:)

相關問題