2013-07-31 66 views
0

我正在研究允許用戶將映像上載到服務器的應用程序。我得到500內部服務器錯誤。我似乎無法找到任何與此錯誤相關的事情,這將解決我的問題。我的代碼如下:500嘗試將映像上載到服務器時的內部服務器

class RetreiveFeedTask extends AsyncTask<String, Void, String> { 
    protected String doInBackground(String... url){ 
     try { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
      Bitmap bitmap = drawable.getBitmap(); 
      bitmap.compress(CompressFormat.JPEG, 50, bos); 
      byte[] data = bos.toByteArray(); 

      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost postRequest = new HttpPost("http://10.155.103.167:9090/RestServer/rest/todos"); 
      String fileName = String.format("File_%d.jpg", new Date().getTime()); 
      ByteArrayBody bab = new ByteArrayBody(data, fileName); 
      ContentBody mimePart = bab; 

      // File file= new File("/mnt/sdcard/forest.png"); 
      // FileBody bin = new FileBody(file); 

      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

      reqEntity.addPart("file", bab); 
      postRequest.setEntity(reqEntity); 
      postRequest.setHeader("Content-Type", "application/json"); 
      int timeoutConnection = 60000; 

      HttpParams httpParameters = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
      int timeoutSocket = 60000; 
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
      HttpConnectionParams.setTcpNoDelay(httpParameters, true); 
      HttpResponse response = httpClient.execute(postRequest); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
      response.getEntity().getContent(), "UTF-8")); 
      String sResponse; 
      StringBuilder s = new StringBuilder(); 
      System.out.println("Response: " + response.getStatusLine()); 

      while ((sResponse = reader.readLine()) != null) { 
       s = s.append(sResponse); 
      } 

      txt.setText("NEW TEXT"+s); 
     } catch (Exception e) { 
      // handle exception here 
      e.printStackTrace(); 
      System.out.println(e.toString()); 
     } 
     return null; 
    } 
} 

回答

0

使用此代碼上傳圖片它的工作對我罰款

public class UploadToServer extends AsyncTask<String, String, String>{ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... args){ 
     String status=""; 
     String URL = ""; 
     try{ 
      Log.d("Image Path ======",TakePicture.file.toString()); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(URL); 
      File file = new File(TakePicture.file.toString()); 
      FileBody bin = new FileBody(file); 
      MultipartEntity reqEntity = new MultipartEntity(); 
      reqEntity.addPart("Content-Disposition", new StringBody("form-data")); 
      reqEntity.addPart("name", new StringBody("Test")); 
      reqEntity.addPart("filename", bin); 
      reqEntity.addPart("Content-Type", new StringBody("image/jpg")); 
      httppost.setEntity(reqEntity); 
      Log.d("Executing Request ", httppost.getRequestLine().toString()); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 
      if (resEntity != null) { 
       Log.d("Response content length: ",resEntity.getContentLength()+""); 
       if(resEntity.getContentLength()>0) { 
        status= EntityUtils.toString(resEntity); 
       } else { 
        status= "No Response from Server"; 
        Log.d("Status----->",status); 
       } 
      } else { 
       status = "No Response from Server"; 
       Log.d("Status----->",status); 
      } 
     } catch (Exception e) { 
       e.printStackTrace(); 
       status = "Unable to connect with server"; 
     } 
      return status; 
     } 

    @Override 
    protected void onPostExecute(String status) { 
     super.onPostExecute(status); 
    } 
} 
+0

你有沒有試過這個代碼 – Developer

0

所有HTTP 5XX代碼表明在服務器端具體問題;你沒有得到像400 Bad Request413 Request Entity Too Large這樣的4xx錯誤,這表明你的客戶端代碼出錯了。服務器上的某些內容出錯了(例如上傳目錄配置錯誤或數據庫連接失敗),並且需要檢查服務器日誌以查看出現的錯誤消息。