开发者

Sending multipart form in android by HttpURLConnection (POST)

I want to send multipart form in my android app but without using org.apache.http.entity.mime so I've created mu own way but it doesn't work the way how I'm creating request:

    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;
}

When the method is called the log information are showing that ByteArrayOutputStream have size 0 to the moment when I'm adding imagedata so it look like the OutputStreamWriter doesn't write strings, or am I wrong?

Any suggestions or solutions?


If someone will look for answer here is it how I made it:

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜