Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All uploaded files used wrong mediatypes #5097

Closed
hantsy opened this issue Jul 16, 2022 · 1 comment · Fixed by #5098
Closed

All uploaded files used wrong mediatypes #5097

hantsy opened this issue Jul 16, 2022 · 1 comment · Fixed by #5098

Comments

@hantsy
Copy link

hantsy commented Jul 16, 2022

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

@jansupol
Copy link
Contributor

Yes, this is a bug. Thank you!

@jansupol jansupol linked a pull request Jul 16, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants