通过REST服务附加PDF内容时出现问题

时间:2017-11-10 19:48:44

标签: java rest pdf java-7

我在将PDF内容附加到REST服务时遇到问题。 REST服务响应成功但我从服务器检索文件时获取空PDF。

使用JSON Content Type进行REST服务调用的代码,并具有基本身份验证机制。

我正在将PDF转换为字节数组,并将其作为字符串附加到HTTP Body内容中。

以下是我的代码 -

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.codec.binary.Base64;

public class AttachPDF {

    static String jsonBody = "{\"property\":\"SAMPLE\",\"propName\":\"SAMPLTEST\"}";

    public static void main(String[] args) throws Exception {

        String authStr = "test" + ":" + "xsyq12";

        // encode data on your side using BASE64
        byte[] bytesEncoded = Base64.encodeBase64(authStr.getBytes());
        String authEncoded = new String(bytesEncoded);

        Path pdfPath = Paths.get("E:\\17251897.pdf");
        byte[] pdf = Files.readAllBytes(pdfPath);
        String pdfContent = new String(pdf);

        URL serverUrl = new URL("http://servicesTest.com/attachPDF");
        HttpURLConnection urlConnection = (HttpURLConnection) serverUrl.openConnection();
        String boundaryString = "----WebKitFormBoundary" + Long.toHexString(System.currentTimeMillis());

        // Indicate that we want to write to the HTTP request body
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);
        urlConnection.setRequestProperty("Authorization", "Basic " + authEncoded);
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestProperty("Connection", "keep-alive");

        // Indicate that we want to write some data as the HTTP request body
        urlConnection.setDoOutput(true);
        OutputStream outputStreamToRequestBody = urlConnection.getOutputStream();
        BufferedWriter httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(outputStreamToRequestBody));

        // Include value from the myFileDescription text area in the post data
        httpRequestBodyWriter.write("\n\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; name=\"attachSource\"" + "\n");
        httpRequestBodyWriter.write("Content-Type: application/json");
        httpRequestBodyWriter.write("\n\n");
        httpRequestBodyWriter.write(jsonBody);
        // Include the section to describe the file
        httpRequestBodyWriter.write("\n--" + boundaryString + "\n");
        httpRequestBodyWriter.write("Content-Disposition: form-data; " + "name=\"file_input\";filename=\"17251897.pdf\""
                + "\nContent-Type: application/pdf\n\n");
        httpRequestBodyWriter.write("file_input:" + pdfContent);

        httpRequestBodyWriter.flush();

        // Mark the end of the multipart http request
        httpRequestBodyWriter.write("\n--" + boundaryString + "--\n");
        httpRequestBodyWriter.flush();

        // Close the streams
        outputStreamToRequestBody.close();
        httpRequestBodyWriter.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
        }
        in.close();
        System.out.println(response.toString());

    }
}

Empty PDF Content

0 个答案:

没有答案