我在尝试使用HTTPPost从json客户端上传包含转换为字节数组的图像的zip文件到一个安静的wcf服务时遇到问题。字节数组被编码到封装在JSON对象中的BASE64中,并使用StringEntity和另外2个参数发送。大约6KB的文件上传没有任何缺陷但是超过6KB的文件没有发送,我得到一个错误的请求 - 400状态代码。以下代码用于上传文件:
文件文件=新文件(dir,“file.zip”);
byte[] buf = new byte[10240000];
fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
Log.v("read : buf ", buf + " : " + readNum + " bytes");
}
byte[] bytes = bos.toByteArray();
imgData = Base64.encodeToString(bytes, Base64.DEFAULT);
JSONObject sendData=null;
Log.d("Image Data length", imgData.length()+"");
Log.d("Image data ", imgData);
try {
sendData= new JSONObject();
sendData.put("_binaryData", imgData);
sendData.put("_fileName", "fileName");
sendData.put("userid", userID);
int len = imgData.length();
int l=sendData.toString().length();
entity = new StringEntity(sendData.toString());
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Send request
int len = imgData.length();
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("Connection", "Keep-Alive");
request.setParams(httpParameters);
DefaultHttpClient httpClient = new DefaultHttpClient();
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
String str=response.getStatusLine().getReasonPhrase();
int i=response.getStatusLine().getStatusCode();
Log.v("ReasonPhrase :: StatusCode",str+" "+i);
int contentLength = (int) responseEntity.getContentLength();
char[] buffer = new char[(int) responseEntity
.getContentLength()];
InputStream stream = responseEntity.getContent();
请帮我解决这个问题。
答案 0 :(得分:0)
如果带有< 6k字节的消息通过,但带有> 6k的消息没有通过,我会查看客户端和主机限制,例如:
MaxBufferSize
MaxBufferPoolSize
MaxReceivedMessageSize
您没有说明您是否可以控制主机服务器设置,但您可以增加和减少前面提到的项目限制。如有必要,您可以将它们设置为Integer.Max,允许文件上传的大小> 1 GB。