gpt4 book ai didi

java - 尝试将图像上传到服务器时出现 500 内部服务器

转载 作者:行者123 更新时间:2023-12-01 14:17:41 26 4
gpt4 key购买 nike

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

最佳答案

所有 HTTP 5xx 代码都明确指出服务器端存在问题;您不会收到 4xx 错误(如 400 Bad Request413 Request Entity Too Large),这表明您的客户端代码执行了错误。服务器上出现问题(例如上传目录配置错误或数据库连接失败),您需要检查服务器日志以查看出现的错误消息。

关于java - 尝试将图像上传到服务器时出现 500 内部服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com