HttpUrlConnection multipart-form-data返回错误422 Unprocessable Entity

时间:2016-04-18 17:40:13

标签: android httpurlconnection

我尝试使用以下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连接构建此表单?

1 个答案:

答案 0 :(得分:0)

最后我成功地构建了它。 这是解决方案

将以下内容添加到草图

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());
        }
    }
相关问题