2011-10-19 64 views
0

下面是我試圖上傳圖片到服務器時得到的runtime execeptionenter image description here上傳圖片到服務器時運行時異常

,我試圖用我的本地服務器(WAMP)和我的Android代碼上傳圖像

Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/Sunset.jpg"); 

    // Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background1); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 

      byte [] byte_arr = stream.toByteArray(); 
      String image_str = Base64.encodeBytes(byte_arr); 

      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

      try{ 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://192.168.1.49/android/upload_image.php"); 

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 

       String the_string_response = convertResponseToString(response); 

       Toast.makeText(uploadimage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show(); 

      }catch(Exception e){ 
        Toast.makeText(uploadimage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); 

        System.out.println("Error http connection"+e.toString()); 
      } 

     } 

     public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ 

      String res = ""; 
      StringBuffer buffer = new StringBuffer(); 

      inputStream = response.getEntity().getContent(); 
      int contentLength = (int) response.getEntity().getContentLength(); //getting content length….. 

      Toast.makeText(uploadimage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show(); 

      if (contentLength < 0){ 
      } 
      else{ 
        byte[] data = new byte[512]; 
        int len = 0; 

        try 
        { 
         while (-1 != (len = inputStream.read(data))) 
         { 

          buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer….. 

         } 

        } 

        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 

        try 
        { 
         inputStream.close(); // closing the stream….. 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
        res = buffer.toString();  // converting string buffer to string 

        Toast.makeText(uploadimage.this, "Result : " + res, Toast.LENGTH_LONG).show(); 

        //System.out.println("Response => " + EntityUtils.toString(response.getEntity())); 

      } 
    return res; 

     } 

} 

,這是我的PHP代碼,這是我從網上找來的。

<?php 

    $base=$_REQUEST['image']; 

    $binary=base64_decode($base); 

    header('Content-Type: bitmap; charset=utf-8'); 

    $file = fopen('uploaded_image.jpg', 'wb'); 

    fwrite($file, $binary); 

    fclose($file); 

    echo 'Image upload complete!!, Please check your php file directory……'; 

?> 

任何人都可以幫助我解決我的問題。在此先感謝

+0

請顯示您的錯誤日誌。 – user370305

回答

0

您必須在c:\中創建一個附加目錄,然後爲其提供讀取,寫入和執行權限。 在你的php代碼中,請寫出存儲位置的絕對路徑。

這可能會解決您的問題。

請記住以管理員模式運行IIS服務器。

0

Inetpub由SYSTEM用戶創建。這意味着您不允許在沒有管理員權限的情況下對其或任何子目錄進行修改。嘗試更改inetpub文件夾的權限,以便任何人都可以修改這些文件。右鍵單擊 - >屬性 - > ...我忘了該怎麼做。 (我現在在Linux中)。如果這不起作用,請確保您以管理員身份運行IIS。

相關問題