2013-02-12 49 views
0

我想將圖片上傳到webService,但我無法做到這一點,我在這裏和在互聯網上搜索了很多話題,但無法找到好的解決方案。我如何將圖片上傳到.net WebService從Android

當我運行此代碼時,我得到錯誤的請求錯誤。

更新:我使用了一些代碼是在這個環節:Uploading MS Word files from Android to .Net WCF?

但給我FileNotFoundException異常,但我的文件路徑是:/mnt/sdcard/ImageDir/images/ilan_1360917248037__thumb_.jpeg

這裏是我的代碼,我想:

public static String imgUpload(String url, List<NameValuePair> list, List<String> images){ 

     String result = Constants.EMPTY; 
     Bitmap bm; 

     HttpParams httpParams = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParams, ServiceConstant.TIMEOUT_MILLISEC); 
     HttpConnectionParams.setSoTimeout(httpParams, ServiceConstant.TIMEOUT_MILLISEC); 

     HttpParams p = new BasicHttpParams(); 
     HttpClient httpclient = new DefaultHttpClient(p); 


     HttpPost httppost = new HttpPost(url);   

     String resimYol = images.get(0); 
     resimYol = resimYol.replace("file:///", "/"); 
     bm = BitmapFactory.decodeFile(resimYol); 
     Log.d("RESIL_YOL", resimYol.toString()); 


     try{       


      ByteArrayBody bab = new ByteArrayBody(b, resimYol); 


      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bm.compress(CompressFormat.JPEG, 100, bos); 
      byte[] data = bos.toByteArray();    

      ByteArrayBody bab = new ByteArrayBody(data, resimYol); 


      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      httppost.setEntity(reqEntity);  
     result = httpclient.execute(httppost,responseHandler); 

     } 

     catch (Exception e) { 
       // handle exception here 
       Log.e(e.getClass().getName(), e.getMessage()); 
      } 
     return result; 
    } 

這裏是我的logcat:

02-12 11:43:07.467: E/org.apache.http.client.HttpResponseException(19112): Bad Request 
+0

可以請你把日誌貓? – 2013-02-12 10:15:25

+1

好吧,我把logcat。 – 2013-02-12 10:16:35

+0

錯誤的請求是服務器說它不喜歡你的POST中的某些東西。檢查服務器是否有某些東西不想獲取請求。 – 2013-02-12 10:40:29

回答

0
public static String __imgUpload(String url, List<NameValuePair> list, List<String> images) 
    { 
     URL url1=null; 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 
     String existingFileName= null; 

      String urlServer = url; 
      String lineEnd = "\r\n"; 
      String twoHyphens = "--"; 
      String boundary = "*****"; 
      String str = null; 

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = Integer.MAX_VALUE; 
     String responseFromServer = ""; 


      try { 
      url1 = new URL(url); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     }     

      for(int resimID=0;resimID<images.size();resimID++) 
      { 
      existingFileName= images.get(resimID); 
      existingFileName = existingFileName.replace("file:///", "/"); 

      try 
      { 
      FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 


       conn = (HttpURLConnection) url1.openConnection(); 

       conn.setDoInput(true); 

       conn.setDoOutput(true); 

       conn.setUseCaches(false); 

       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("Content-Type", "application/stream"); 
       dos = new DataOutputStream(conn.getOutputStream()); 

       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       buffer = new byte[bufferSize]; 
       // read file and write it into form... 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
       while (bytesRead > 0) 
       { 
       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       } 

       dos.writeBytes(lineEnd); 


       // close streams 
       Log.e("Debug",twoHyphens + boundary + twoHyphens + lineEnd); 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 
      } 
      catch (MalformedURLException ex) 
      { 
        Log.e("Debug", "error 1: " + ex.getMessage(), ex); 
      } 
      catch (IOException ioe) 
      { 
        Log.e("Debug", "error 2: " + ioe.getMessage(), ioe); 
      } 

      try { 
        inStream = new DataInputStream (conn.getInputStream()); 


        while ((str = inStream.readLine()) != null) 
        { 
         Log.e("Debug","Server Response "+str); 
        } 
        inStream.close(); 

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

      return str; 
    } 

此代碼絕對有效。

0

試試這個例子Link它可能會幫助你。

並添加所需的所有jar文件。

+0

好吧,我會嘗試,但沒有任何參數類型或數據在.net webService端。所以我不知道我會如何上傳照片? – 2013-02-12 10:31:08

+0

你的參數就像List 列表。 – 2013-02-12 10:33:53

+0

列表變量是不同的。我爲圖片使用了「圖片」變量。 – 2013-02-12 11:40:34

0

嘗試轉換Base64中的image,然後將其作爲參數的String響應作爲參數發送到Web服務,以成功上載到服務器。

+0

作爲參數? image = fasfosdsd.jpg或類似的東西?但是服務器端沒有任何參數。 – 2013-02-12 11:41:20

+0

好的。服務器請求發送圖像到底是什麼?我的意思是你直接需要發送JPG文件或先以某種格式轉換它? – Harpreet 2013-02-12 12:37:58

+0

服務器需要字節格式的圖像文件。 – 2013-02-13 11:37:29