我想在我的 android 应用程序中发送多部分表单,但不使用 org.apache.http.entity.mime 所以我创建了 mu 自己的方式,但它不像我创建请求的方式那样工作:
public byte[] createRequest(byte[] imagedata){
byte[] requestData = null;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
OutputStreamWriter output = null;
try {
Log.i(TAG, "Creating request");
output = new OutputStreamWriter(buf, "UTF-8");
output.write("--");
output.write(boundary);
output.write("\r\n");
output.write("Content-Disposition: form-data; name=\"auth\"; filename=\"auth\"\r\n");
output.write("Content-Type: text/xml; charset=utf-8\r\n");
output.write("\r\n");
byte temp2[] = buf.toByteArray();
Log.i(TAG, "BUF SIZE: " + temp2.length);
Log.i(TAG, "BUF: " + buf.toString());
ByteArrayOutputStream authBuffer = new ByteArrayOutputStream();
OutputStreamWriter authOut = new OutputStreamWriter(authBuffer, "UTF-8");
writeAuthRequestFragment(user, pass, company_id, "RESLINK CLIENT", "2.0", null, null, null, authOut);
buf.write(authBuffer.toByteArray());
Log.i(TAG, "BUF SIZE: " + buf.size());
output.write("\r\n--" + boundary + "\r\n");
output.write("Content-Disposition: form-data; name=\""+CIMAGE+"\"; filename=\""+CIMAGE+"\"\r\n");
output.write("Content-Type: "+IMAGE_PNG+"\r\n");
output.write("\r\n");
buf.write(imagedata);
output.write("\r\n--" + boundary + "--\r\n");
requestData = buf.toByteArray();
Log.i(TAG, "WHOLE SIZE " + requestData.length);
output.flush();
} catch(IOException ex){
ex.printStackTrace();
}
finally{
if(buf!=null){
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(output!=null){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return requestData;
}
当调用该方法时,日志信息显示 ByteArrayOutputStream 的大小为 0 到我添加图像数据的那一刻,所以看起来 OutputStreamWriter 没有写入字符串,还是我错了?
有什么建议或解决方案吗?