我在服务器上发布图片时遇到问题。我正在使用httpConnection,并且遇到连接关闭,未连接,TCP超时,空例外等问题。发布时,我发布了一个JSON对象,这些问题发生不一致,但随机发生,我无法理解问题的根本原因?
byte[] buffer = new byte[20000];
int bytesRead = responseData.read(buffer);
while (bytesRead > 0) {
byteArray.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}
byteArray.close();
我替换为
byte[] byteArray = IOUtilities.streamToBytes(request.openInputStream());
我试图了解这些变化是否有用?
我发布了用于发布图片的示例代码
request = (HttpConnection) connect.getConnection();
request.setRequestMethod("POST");
request.setRequestProperty("Content-Type", "application/json");
os = request.openOutputStream();
JSONObject jsonObjSMS;
Vector dataValue = new Vector();//Vector for putting Images
try {
jsonObjSMS = new JSONObject();
jsonObjSMS.put("strNo", MyObject.getNumber());
byte[] encoded = Base64OutputStream.encode(dataValues, 0,dataValues.length,false, false);
String baseEncodedStringArrayValue = new String(encoded, "UTF-8");
dataValue.addElement(baseEncodedStringArrayValue);
jsonObjSMS.put("imageArray", dataValue);
}catch(Exception e){
System.out.println("Exception in JSON"+e);
}
os.write(jsonObjSMS.toString().getBytes("UTF-8"));
os.flush();
byte[] byteArray = IOUtilities.streamToBytes(request.openInputStream());
jsonResponse = new String(byteArray, "UTF-8");
我是否遗漏了随机创建此错误的任何代码?