需要将捕获图像发送到android中的服务器,我发送的图像是字符串,代码相同:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
String strBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
我遇到的问题是strBase64长度太大,发送图像的代码是:
private boolean post(Context context, String message) {
HttpURLConnection urlConnection = null;
try {
URL _url = new URL(server_url);
urlConnection = (HttpURLConnection) _url.openConnection();
urlConnection.setReadTimeout(1 * 60 * 1000);
urlConnection.setConnectTimeout(1 * 60 * 1000);
urlConnection.setRequestMethod("POST");
urlConnection
.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Content-Length", message
.toString().length() + "");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(
urlConnection.getOutputStream());
byte[] bs = message.getBytes();
dos.write(bs);
dos.flush();
dos.close();
if (urlConnection.getResponseMessage().toLowerCase().equals("ok")) {
InputStream is = urlConnection.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
// responseString = b.toString();
return true;
} // data sent successfully
dos.close();
} catch (IOException ioe) {
return false;
}
return false;
}
如何增加httpurlconnection的大小以便它也可以接受长字符串?
Reponse Error logcat:
07-24 18:53:54.110: E/Response(14602): ResponseCode ->400
07-24 18:53:54.110: E/Response(14602): ResponseMessage ->Bad Request
答案 0 :(得分:0)
我自己没试过,但您可以尝试将内容类型更改为"应用程序/文字"
答案 1 :(得分:0)
使用此方法将图像上传到服务器。我在我的一个项目中使用过这个方法你也可以试试。在这段代码中我设置限制,如果图像大小超过2MB,则只上传2MB图像,然后压缩图像并上传。
Footer
//这是多部门类
DateTime end = DateTime.Now;
DateTime start = end.AddDays(-30);
DateTime current = start;
List<DateTime> result = new List<DateTime>();
while (currrent <= end)
{
result.Add(current);
current = current.AddDays(1);
}
希望它会对你有所帮助。如果您对此有任何疑问,可以问我。 谢谢:))