栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java文件上传以及Jersey静态Web服务中的其他对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java文件上传以及Jersey静态Web服务中的其他对象

你不能有两个

Content-Types
(从技术上讲,这是我们在下面执行的操作,但是它们与
multipart
的每个部分都分开,但是主要类型是
multipart
)。这基本上就是你对方法的期望。你期望
mutlipart
json
一起作为主要媒体类型。该Employee数据需要多部分的一部分。所以,你可以添加
@FormDataParam("emp")
Employee

@FormDataParam("emp") Employee emp) { ...

这是我用来测试的课程

@Path("/multipart")public class MultipartResource {    @POST    @Path("/upload2")    @Consumes({MediaType.MULTIPART_FORM_DATA})    public Response uploadFileWithData( @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition cdh, @FormDataParam("emp") Employee emp) throws Exception{        Image img = ImageIO.read(fileInputStream);        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));        System.out.println(cdh.getName());        System.out.println(emp);        return Response.ok("Cool Tools!").build();    } }

首先,我刚刚使用客户端API进行了测试,以确保它可以正常工作

@Testpublic void testGetIt() throws Exception {    final Client client = ClientBuilder.newBuilder()        .register(MultiPartFeature.class)        .build();    WebTarget t = client.target(Main.base_URI).path("multipart").path("upload2");    FileDataBodyPart filePart = new FileDataBodyPart("file",  new File("stackoverflow.png"));    // UPDATE: just tested again, and the below pre is not needed.    // It's redundant. Using the FileDataBodyPart already sets the    // Content-Disposition information    filePart.setContentDisposition( FormDataContentDisposition.name("file")   .fileName("stackoverflow.png").build());    String empPartJson = "{" + "  "id": 1234," + "  "name": "Peeskillet"" + "}";    MultiPart multipartEntity = new FormDataMultiPart() .field("emp", empPartJson, MediaType.APPLICATION_JSON_TYPE) .bodyPart(filePart);    Response response = t.request().post( Entity.entity(multipartEntity, multipartEntity.getMediaType()));    System.out.println(response.getStatus());    System.out.println(response.readEntity(String.class));    response.close();}

我刚刚创建了一个简单的Employee类,id并带有and name字段进行测试。这工作得很好。它显示图像,打印内容配置,然后打印Employee对象。

我对Postman不太熟悉,所以我将测试保存到最后

如你所见,它似乎也可以正常工作”Cool Tools”。但是,如果我们查看打印的Employee数据,就会发现它为空。这很奇怪,因为使用客户端API可以正常工作。

如果我们查看“预览”窗口,就会发现问题所在

主体部分没有

Content-Type
标题
emp
。你可以在客户端API中看到我明确设置了它

MultiPart multipartEntity = new FormDataMultiPart()        .field("emp", empPartJson, MediaType.APPLICATION_JSON_TYPE)        .bodyPart(filePart);

因此,我想这实际上只是完整答案的一部分。就像我说的,我对邮递员不熟悉,所以我不知道如何Content-Type为身体的各个部位设置s。在image/png对图像进行自动为我设置的图像部分(我猜它只是由文件扩展名确定)。如果你能解决这个问题,那么应该解决问题。请,如果你知道如何执行此操作,请将其发布为答案。

And just for completeness…

请参阅此处以了解更多有关MultiPart with Jersey的信息。
基本配置:

依赖关系:

<dependency>    <groupId>org.glassfish.jersey.media</groupId>    <artifactId>jersey-media-multipart</artifactId>    <version>${jersey2.version}</version></dependency>

客户端配置:

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

服务器配置:

// Create JAX-RS application.final Application application = new ResourceConfig()    .packages("org.glassfish.jersey.examples.multipart")    .register(MultiPartFeature.class);

UPDATE

因此,从Postman客户端可以看到,某些客户端无法设置各个部分的Content-Type,包括浏览器,这是使用FormData(js)时的默认功能。

我们不能指望客户能找到解决办法,因此我们可以做的就是在接收数据时在反序列化之前显式设置Content-Type。例如

@POST@Path("upload2")@Consumes(MediaType.MULTIPART_FORM_DATA)public Response uploadFileAndJSON(@FormDataParam("emp") FormDataBodyPart jsonPart, @FormDataParam("file") FormDataBodyPart bodyPart) {      jsonPart.setMediaType(MediaType.APPLICATION_JSON_TYPE);     Employee emp = jsonPart.getValueAs(Employee.class);}

获取POJO会花费一些额外的工作,但是比强迫客户端尝试找到自己的解决方案要好。

Asides

如果你使用的连接器不同于默认的HttpUrlConnection,则你可能会对这些注释中的对话感兴趣。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号