在javax请求中发布两个InputStream

时间:2018-10-26 17:20:25

标签: java rest resteasy

我正在尝试向具有两个文件形式的端点发送请求。我当前尝试的方法在最后一行失败:

DATEADD

达到的错误是

WebTarget client = myUtils.createClient(URL, ENDPOINT);

MultivaluedMap<String, InputStream> formData = new MultivaluedHashMap<>();
formData.add(FILE_1, stream1);
formData.add(FILE_2, stream2);

Entity<MultivaluedMap<String, InputStream>> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE);

Response response = client.request().post(entity);

javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type multipart/form-data type: javax.ws.rs.core.MultivaluedHashMap 更改为MediaType会产生

APPLICATION_FORM_URLENCODED_TYPE

是否有更好的方法来处理包含两个文件的表单?

1 个答案:

答案 0 :(得分:0)

有多种方法。 如果您使用的是Jersey,一种方法是执行以下操作:

  TableName    ColumnName
0    Table1    column11
1    Table1    Column12
2    Table2    Column21
3    Table2    Column22
4    Table3    Column23

另一种方法是使用Apache HttpUtils

import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
....
....
   FileDataBodyPart fileDataBodyPart = 
             new FileDataBodyPart("file", new File("/filelocation/file.txt"));
    FormDataMultiPart formDataMultiPart = (FormDataMultiPart) 
                        FormDataMultiPart.field("somekey","somevalue")
                             .bodyPart( fileDataBodyPart);

     WebTarget target = client.target(restServiceURLYouwant);
     Response response = target.request().post(Entity.entity(formDataMultiPart, 
      formDataMultiPart.getMediaType()));

       formDataMultiPart.close();