简体   繁体   English

如何构造正确的MultipartEntity以在Java中发送多部分/相关请求?

[英]How to construct correct MultipartEntity to send a multipart/related request in java?

I want to send to OCR image from local computer, not some remote url with image there (this case working great). 我想从本地计算机发送到OCR图像,而不是从那里有图像的远程URL(这种情况下效果很好)。 But the problem is, I don't know how construct correctly payload of multipart entity. 但是问题是,我不知道如何正确构造多部分实体的有效负载。 My payload should be looking as it described below. 我的有效负载应如下所述。

This's what described in documentation of API . 这就是API文档中描述的内容 Decode the image data given directly in the multipart/related data. 解码直接在多部分/相关数据中给出的图像数据。 The order is important, and the first part should be the JSON, which tells it which OCR engine to use. 顺序很重要,第一部分应该是JSON,它告诉它使用哪个OCR引擎。 The schema for thi JSON is documented in the /ocr endpoint. JSON的模式记录在/ocr端点中。 The img_url parameter of the JSON will be ignored in this case. 在这种情况下,将忽略JSON的img_url参数。

The image attachment should be the second part, and it should work with any image content type (eg, image/png, image/jpg, etc). 图像附件应为第二部分,并且应适用于任何图像内容类型(例如image / png,image / jpg等)。

   Request (multipart/related; boundary=---BOUNDARY)

    -----BOUNDARY
    Content-Type: application/json

    {"engine":"tesseract"}
    -----BOUNDARY

    -----BOUNDARY
    Content-Disposition: attachment;
    Content-Type: image/png
    filename="attachment.txt".

    PNGDATA.........
    -----BOUNDARY

Here's what I've tried. 这是我尝试过的。 To perform multipart/related request I am using org.apache.httpcomponents 要执行多部分/相关请求,我正在使用org.apache.httpcomponents

    CloseableHttpClient httpClient = HttpClients.createDefault();

    MultipartEntityBuilder multipartEntityBuilder =
            MultipartEntityBuilder.create().setBoundary(BOUNDARY).setContentType(ContentType.APPLICATION_JSON).addTextBody("engine", "tesseract")
                                                    .setBoundary(BOUNDARY).setBoundary(BOUNDARY);

    multipartEntityBuilder.addBinaryBody("file_upload", new File(fileTextPath), ContentType.create(CONTENT_TYPE), fileTextPath).setBoundary(BOUNDARY);

    HttpEntity entity = multipartEntityBuilder.build();


    HttpPost httpPost = new HttpPost(URL);
    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_MULTIPART+";boundary="+BOUNDARY);
    httpPost.setEntity(entity);

I resolved this problem a few weeks ago and for convenience created a lightweight java web application for both cases (remote and file upload). 几周前我解决了这个问题,为方便起见,为这两种情况(远程和文件上传)创建了一个轻量级的Java Web应用程序

You can find the particular answer for the question above here . 您可以在此处找到上述问题的特定答案。 The source code for a multipart request is shown below: 多部分请求的源代码如下所示:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class MultipartUtility {
    private static final Logger logger = LoggerFactory.getLogger(MultipartUtility.class);

    private final String boundary = UUID.randomUUID().toString();
    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
     * @param charset
     * @throws IOException
     */
    public MultipartUtility(String requestURL, String charset) {
        this.charset = charset;

        try {
            URL url = new URL(requestURL);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setUseCaches(false);
            httpConn.setDoOutput(true); // indicates POST method
            httpConn.setDoInput(true);
            httpConn.setRequestProperty("Content-Type", "multipart/related; boundary=\"" + boundary + "\"");
            httpConn.setRequestProperty("Accept-Encoding", "gzip");
            outputStream = httpConn.getOutputStream();
            //outputStream = System.out;
            writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
        } catch (IOException ex) {
            logger.error("Error during creation of MultiPart: ", ex);
        }
    }

    /**
     * Adds a form field to the request
     */
    public void addFormField(String jsonBody) {
        writer.append("--").append(boundary).append(LINE_FEED);
        writer.append("Content-Type: application/json;").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(jsonBody).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a upload file section to the request
     *
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(File uploadFile) {
        String fileName = uploadFile.getName();
        writer.append("--").append(boundary).append(LINE_FEED);
        writer.append("Content-Disposition: attachment;");
        writer.append(" filename=\"" + fileName + "\".").append(LINE_FEED);
        writer.append("Content-Type: image/*").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        try {
            FileInputStream inputStream = new FileInputStream(uploadFile);
            //byte[] buffer = new byte[(int) uploadFile.length()];
            byte[] buffer = Files.readAllBytes(Paths.get(uploadFile.getPath()));
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.flush();
            inputStream.close();
        } catch (IOException ex) {
            logger.error("File transformation to bytes went wrong: {}", ex);
        }

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

    /**
     * Adds a header field to the request.
     *
     * @param name  - name of the header field
     * @param value - value of the header field
     */
    public void addHeaderField(String name, String value) {
        writer.append(name + ": " + value).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 String finish() {
        String response = "";
        int status = 0;

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

        try {
            // checks server's status code first
            status = httpConn.getResponseCode();

            if (status == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

                String line;
                while ((line = reader.readLine()) != null) {
                    response += line;
                }

                reader.close();
                httpConn.disconnect();
            } else {
                logger.error("OCR API returned error stream: {}", printErrorStream());
                throw new IOException("Server returned non-OK status: " + status + " : " + httpConn.getResponseMessage());
                //logger.error("Server returned non-OK status: " + status + " : " + httpConn.getResponseMessage());
            }
        } catch(IOException ex) {
            logger.error("Response message in Multipart finish has been received with problems: ", ex);
        }

        return response;
    }

    private String printErrorStream() throws IOException {
        //System.out.print("DEBUG System out ocr API error stream: ");
        InputStream errorStream = httpConn.getErrorStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream));
        String errLine = "", tempLine;
        while ((tempLine = reader.readLine()) != null) {
            errLine += tempLine;
        }

        return errLine;
    }

    private void getRequestHeaders(HttpURLConnection httpURLConnection) {
        for (Map.Entry<String, List<String>> entries : httpURLConnection.getRequestProperties().entrySet()) {
            String values = "";
            for (String value : entries.getValue()) {
                values += value + ",";
            }
            System.out.println("Request" + " " + entries.getKey() + " - " +  values );
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM