简体   繁体   中英

Posting a form using apache cxf

I am trying to post a form which contains file as well as normal text data. I am using Apache CXF for this. Below is the code

WebClient client= WebClient.create(ROOT_URL_FILE_SERVICE);

        client.type("multipart/form-data");     
        InputStream is= new ByteArrayInputStream(getBytes());       
        List<Attachment> attachments= new ArrayList();
        ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
        Attachment att= new Attachment("File", is, cd);
        Attachment pageNumber= new Attachment("DATA1", MediaType.TEXT_PLAIN, "1");
        Attachment OutputType= new Attachment("DATA2", MediaType.TEXT_PLAIN, "2");

        attachments.add(att);
        attachments.add(pageNumber);
        attachments.add(OutputType);
        MultipartBody body= new MultipartBody(attachments);

        Response res=client.post(body);

I am unable to get anything (File,DATA1,DATA2) on server side.

What is that i need to modify in above code to get it working.

Check your server side cxf configuration is something like below.

@POST
@Path("/upload")
@Produces(MediaType.TEXT_XML)
public String upload(
        @Multipart(value = "File", type = MediaType.APPLICATION_OCTET_STREAM) final InputStream fileStream,
        @Multipart(value = "DATA1", type = MediaType.TEXT_PLAIN) final String fileNumber,
        @Multipart(value = "DATA2", type = MediaType.TEXT_PLAIN) final String outputType) {
    BufferedImage image;
    try {
        image = ImageIO.read(fileStream);
        LOG.info("Received Image with dimensions {}x{} ", image.getWidth(), image.getHeight());
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }

    LOG.info("Received Multipart data1 {} ", fileNumber);
    LOG.info("Received Multipart data2 {} ", outputType);
    return "Recieved all data";
}

The tested client file

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

            WebClient client = WebClient.create("http://localhost:8080/services/kp/upload");
            ClientConfiguration config = WebClient.getConfig(client);
            config.getInInterceptors().add(new LoggingInInterceptor());
            config.getOutInterceptors().add(new LoggingOutInterceptor());
            client.type("multipart/form-data");
            InputStream is = FileUtils.openInputStream(new File("vCenter_del.jpg"));
            List<Attachment> attachments = new ArrayList<>();
            ContentDisposition cd = new ContentDisposition("attachment;filename=image.jpg");
            Attachment att = new Attachment("File", is, cd);
            Attachment pageNumber = new Attachment("DATA1", MediaType.TEXT_PLAIN, "1");
            Attachment OutputType = new Attachment("DATA2", MediaType.TEXT_PLAIN, "2");

            attachments.add(att);
            attachments.add(pageNumber);
            attachments.add(OutputType);
            MultipartBody body = new MultipartBody(attachments);

            Response res = client.post(body);

            String data = res.readEntity(String.class);
            System.out.println(data);
        }

NOTE: In short if there is mismatch in content-id that if file,data1 or content-type, server might not recieve the data in both cases you will get appropriate error such as 400 and 415 respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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