通过HttpURLConnection(POST)在android中发送多部分表单

时间:2011-03-15 08:26:43

标签: android http post multipartform-data

我想在我的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,直到我添加imagedata,所以看起来OutputStreamWriter不会写字符串,或者我错了吗?

有任何建议或解决方案吗?

1 个答案:

答案 0 :(得分:5)

如果有人会在这里寻找答案,那我就是这样做的:

private byte[] generatePhotoRequest(byte[] imagedata){
    byte[] requestData = null;
    ByteArrayOutputStream bufer = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(bufer);
    try{
        dataOut.writeBytes("--");
        dataOut.writeBytes(BOUNDARY);
        dataOut.writeBytes("\r\n");
        dataOut.writeBytes("Content-Disposition: form-data; name=\"auth\"; filename=\"auth\"\r\n");
        dataOut.writeBytes("Content-Type: text/xml; charset=utf-8\r\n");
        dataOut.writeBytes("\r\n");
        dataOut.write(generateAuth());
        dataOut.writeBytes("\r\n--" + BOUNDARY + "\r\n");
        dataOut.writeBytes("Content-Disposition: form-data; name=\""+CIMAGE+"\"; filename=\""+CIMAGE+"\"\r\n");
        dataOut.writeBytes("Content-Type: "+IMAGE_PNG+"\r\n");
        dataOut.writeBytes("\r\n");
        bufer.write(imagedata);
        dataOut.writeBytes("\r\n");
        dataOut.writeBytes("\r\n--" + BOUNDARY + "--\r\n");

        requestData = bufer.toByteArray();
    } catch(IOException ex){
        ex.printStackTrace();
    } finally{
        if(bufer!=null){
            try {
                bufer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return requestData;
}