在RESTFUL(JERSEY)中作为MULTIPART-FORMDATA发送的pdf内容中的额外信息

时间:2016-05-25 08:19:50

标签: rest pdf jersey content-type multipartform-data

使用JERSEY客户端上传PDF /文本文件时,额外信息将附加到原始pdf文件中。(我使用Jersey Glassfish API for Client)

以下是保存在服务器中时附加到原始文件的额外内容:

  --Boundary_1_10166575_1464163574882
   Content-Type: text/plain
   Content-Disposition: form-data; name="foo" bar

   --Boundary_1_10166575_1464163574882
    Content-Type: application/pdf
    Content-Disposition: form-data; filename="ABC.pdf"; modification-date="Tue, 03 May 2016 06:34:59 GMT"; size=109494; name="inputPdfFile"

=============================================== ========================== 请参阅以下客户端源代码:

        final Client client = ClientBuilder.newBuilder()
                .register(MultiPartFeature.class).build();

        final FileDataBodyPart filePart = new FileDataBodyPart(
                "inputPdfFile", new File(
                        "C:/Test/ABC.pdf"));
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
                .field("foo", "bar").bodyPart(filePart);

        final WebTarget target = client
                .target("http://localhost:7001/RestFulService/resources/pdfUploadTest");
        final Response response = target.request().post(
                Entity.entity(multipart, multipart.getMediaType()));

=============================================== ========================= 请参阅以下服务器源代码:

@POST
@Path("/pdfUploadTest")
@Consumes(MediaType.MULTIPART_FORM_DATA)    
public Response uploadFile(
    @FormDataParam("inputPdfFile") InputStream encodedPdfData,
    @FormDataParam("inputPdfFile") FormDataContentDisposition fileDetail) {

           OutputStream out = new FileOutputStream(new   
                                 File("C:/Test/success.pdf"));
         byte[] byteArray = new byte[1024];
        if (null != encodedPdfData) {
            while ((x = encodedPdfData.read(byteArray)) != -1) {
                out.write(byteArray,0,x);
            }
        }
        out.flush();
        out.close();
    }

0 个答案:

没有答案