Skip to content

All uploaded files used wrong mediatypes #5097

@hantsy

Description

@hantsy

Glassfish 7.0M6, Java 17, Windows 10.

I used the following client codes to upload files,

List<EntityPart> parts = List.of(
                EntityPart.withName("textFile").fileName("test.txt")
                        .content(this.getClass().getResourceAsStream("/test.txt"))
                        .mediaType(MediaType.TEXT_PLAIN_TYPE)
                        .build(),
                EntityPart.withName("imageFile").fileName("test.svg")
                        .content(this.getClass().getResourceAsStream("/test.svg"))
                        .mediaType(MediaType.APPLICATION_SVG_XML_TYPE)
                        .build()
        );
        var genericEntity = new GenericEntity<List<EntityPart>>(parts) {
        };
        var entity = Entity.entity(genericEntity, MediaType.MULTIPART_FORM_DATA);
        Response r = target.request().post(entity);

The server side codes are like this.

 @Path("list")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadMultiFiles(List<EntityPart> parts) {
        LOGGER.log(Level.INFO, "Uploading files: {0}", parts.size());
        parts.forEach(
                part -> {
                    LOGGER.log(
                            Level.INFO,
                            "{0},{1},{2},{3}",
                            new Object[]{
                                    part.getMediaType(),
                                    part.getName(),
                                    part.getFileName(),
                                    part.getHeaders()
                            }
                    );
                    try {
                        copy(part.getContent(), Paths.get(uploadedPath.toString(), part.getFileName().get()), StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
        );

        return Response.ok().build();
    }

Running the client in tests, in the Glassfish server.log, all mediatypes are changed to application/octet-stream.

[2022-07-16T23:28:38.775+0800] [glassfish 7.0] [INFO] [] [com.example.MultipartResource] [tid: _ThreadID=41 _ThreadName=http-listener-1(4)] [timeMillis: 1657985318775] [levelValue: 800] [[
  Uploading files: 2]]

[2022-07-16T23:28:38.778+0800] [glassfish 7.0] [INFO] [] [com.example.MultipartResource] [tid: _ThreadID=41 _ThreadName=http-listener-1(4)] [timeMillis: 1657985318778] [levelValue: 800] [[
  application/octet-stream,textFile,Optional[test.txt],{Content-Type=[application/octet-stream], Content-Disposition=[form-data; filename="test.txt"; name="textFile"]}]]

[2022-07-16T23:28:38.787+0800] [glassfish 7.0] [INFO] [] [com.example.MultipartResource] [tid: _ThreadID=41 _ThreadName=http-listener-1(4)] [timeMillis: 1657985318787] [levelValue: 800] [[
  application/octet-stream,imageFile,Optional[test.svg],{Content-Type=[application/octet-stream], Content-Disposition=[form-data; filename="test.svg"; name="imageFile"]}]]

The example project to produce the issue: https://github.com/hantsy/jakartaee10-sandbox/tree/master/rest

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions