【问题标题】:HttpUrlConnection multipart-form-data return error 422 Unprocessable EntityHttpUrlConnection multipart-form-data 返回错误 422 无法处理的实体
【发布时间】:2016-04-18 17:40:13
【问题描述】:

我尝试使用以下3个类使用多部分表单数据上传图像,服务器响应返回状态码422 Unprocessable Entity,我正在尝试上传JPG图片。 我试图谷歌它以找到引发此错误的原因,但我没有找到解决方案。

代码 1:

    public class MultipartUtility {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     * @param requestURL request url
     * @param charset charset as utf-8
     * @throws IOException
     */
    public MultipartUtility(String requestURL, String charset, String auth)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("access_token", auth);
        outputStream = httpConn.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
    }

    /**
     * Adds a form field to the request
     * @param name field name
     * @param value field value
     */
    public void addFormField(String name, String value) {
        writer.append("--").append(boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"")
                .append(LINE_FEED);
        writer.append("Content-Type: text/plain; charset=").append(charset).append(
                LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(value).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a upload file section to the request
     * @param fieldName name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = uploadFile.getName();
        writer.append("--").append(boundary).append(LINE_FEED);
        writer.append(
                "Content-Disposition: form-data; name=\"")
                .append(fieldName)
                .append("\"; filename=\"").append(fileName).append("\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: ")
                .append(URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        inputStream.close();

        writer.append(LINE_FEED);
        writer.flush();
    }

    /**
     * Completes the request and receives response from the server.
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<>();

        writer.append(LINE_FEED).flush();
        writer.append("--").append(boundary).append("--").append(LINE_FEED);
        writer.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
        return response;
    }
}

代码 2:

public class MultipartUtility {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;

    public MultipartUtility(String requestURL, String charset, String auth)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("access_token", auth);
        outputStream = httpConn.getOutputStream();


    }

    public void addFormField(String name, String value) throws IOException {
        outputStream.write(("--" + boundary + LINE_FEED).getBytes(Charset.forName("UTF-8")));

        outputStream.write("Content-Disposition: form-data; name=\"".getBytes(Charset.forName("UTF-8")));
        outputStream.write((name + "\"" + LINE_FEED).getBytes(Charset.forName("UTF-8")));
        outputStream.write(("Content-Type: text/plain; charset=" + charset + LINE_FEED).getBytes(Charset.forName("UTF-8")));

        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));
        outputStream.write(value.getBytes(Charset.forName("UTF-8")));
        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));
        outputStream.flush();
    }

    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = uploadFile.getName();
        outputStream.write("--".getBytes(Charset.forName("UTF-8")));
        outputStream.write(boundary.getBytes(Charset.forName("UTF-8")));
        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));

        outputStream.write("Content-Disposition: form-data; name=\"".getBytes(Charset.forName("UTF-8")));
        outputStream.write(fieldName.getBytes(Charset.forName("UTF-8")));
        outputStream.write(("\"; filename=\"" + "\"").getBytes(Charset.forName("UTF-8")));
        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));

        outputStream.write("Content-Type: ".getBytes(Charset.forName("UTF-8")));
        outputStream.write(URLConnection.guessContentTypeFromName(fileName).getBytes(Charset.forName("UTF-8")));
        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));

        outputStream.write("Content-Transfer-Encoding: binary".getBytes(Charset.forName("UTF-8")));
        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.flush();

        inputStream.close();
    }

    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<>();

        outputStream.write(LINE_FEED.getBytes(Charset.forName("UTF-8")));
        outputStream.write(("--"+boundary+("--")).getBytes(Charset.forName("UTF-8")));
        outputStream.flush();
        outputStream.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }

        return response;
    }
}

代码 3:

public class MultipartUtility_v2 {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private DataOutputStream request;

    public MultipartUtility_v2(String requestURL, String charset, String auth)
            throws IOException {
        this.charset = charset;

        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("access_token", auth);

        request = new DataOutputStream(
                httpConn.getOutputStream());
    }

    public void addFormField(String name, String value) throws IOException {
        request.writeBytes("--" + boundary + LINE_FEED);

        request.writeBytes("Content-Disposition: form-data; name=\"" + boundary + LINE_FEED);
        request.writeBytes(name + "\"" + LINE_FEED);
        request.writeBytes("Content-Type: text/plain; charset=" + charset + LINE_FEED);

        request.writeBytes(LINE_FEED);
        request.writeBytes(value);
        request.writeBytes(LINE_FEED);
    }

    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = uploadFile.getName();

        request.writeBytes("--" + boundary + LINE_FEED);

        request.writeBytes("Content-Disposition: form-data; name=\"");
        request.writeBytes(fieldName);
        request.writeBytes("\"; filename=\"" + "\"");
        request.writeBytes(LINE_FEED);

        request.writeBytes("Content-Type: ");
        request.writeBytes(URLConnection.guessContentTypeFromName(fileName));
        request.writeBytes(LINE_FEED);

        request.writeBytes("Content-Transfer-Encoding: binary");
        request.writeBytes(LINE_FEED);

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            request.write(buffer, 0, bytesRead);
        }
        inputStream.close();
    }

    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<>();

        request.writeBytes(LINE_FEED);
        request.writeBytes("--"+boundary+("--"));

        request.flush();
        request.close();

        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }

        return response;
    }
}

我尝试使用这三种方式发送多部分表单/数据,并在这三种方式中收到错误代码 422。

我正在尝试构建如下形式

<form action="http://ip:port/***/****/****/" method="POST" enctype="multipart/form-data">
<input type="hidden" id="postId" name="postId" value="1"/ >
<input type="hidden" id="type" name="type" value="image"/ >
<input type="file" id="file" name="file"/ >
<input type="submit" id="upload"/>
</form>

任何人都可以帮助我为什么会发生这个错误?以及如何使用 HTTP URL 连接构建此表单?

【问题讨论】:

  • 您不能同时写入 outputStream 和 writer。仅使用一个输出流。取消 PrintWriter。
  • 我没听懂你,请编码你帮我做更多的声明?我必须更改哪一行,以及如何更改?
  • 我已经用 DataOutputStream 更改了编写器,但我仍然遇到同样的问题。
  • 请出示您现在使用的代码。编辑您的帖子。

标签: android httpurlconnection


【解决方案1】:

我终于成功构建了它。 这就是解决方案

将以下内容添加到 gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    useLibrary  'org.apache.http.legacy'
    ...
    dependencies {
             compile 'org.apache.httpcomponents:httpmime:4.3.1'
             compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

并添加以下函数

public void executeMultipartPost(Bitmap bm) throws Exception {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG, 75, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(url);
            ByteArrayBody bab = new ByteArrayBody(data, getString(R.string.file_path));

            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            postRequest.addHeader("access_token", auth);

            reqEntity.addPart("type", new StringBody("image"));
            reqEntity.addPart("postId", new StringBody(postId));
            reqEntity.addPart("file", bab);


            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            System.out.println("Response: " + s);
        } catch (Exception e) {
            // handle exception here
            Log.e(e.getClass().getName(), e.getMessage());
        }
    }

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2015-11-18
    相关资源
    最近更新 更多