其他分享
首页 > 其他分享> > 通过HttpURLConnection(POST)在android中发送多部分表单

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

作者:互联网

我想在我的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不会写字符串,还是我错了吗?

有什么建议或解决方案吗?

解决方法:

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

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

标签:post,http,multipartform-data,android
来源: https://codeday.me/bug/20191023/1912927.html