500尝试将图像上载到服务器时的内部服务器

时间:2013-07-31 05:50:53

标签: java android

我正在开发一个允许用户将图像上传到服务器的应用程序。我收到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;
    }
}

2 个答案:

答案 0 :(得分: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); 
    }
}

答案 1 :(得分:0)

所有HTTP 5xx代码都明确指出服务器端的 ;您没有得到像400 Bad Request413 Request Entity Too Large这样的4xx错误,表明您的客户端代码出错了。服务器上的某些内容出错(例如配置错误的上载目录或数据库连接失败),您需要检查服务器日志以查看出现的错误消息。