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

Spark Java - strange behavior when uploading files #1265

Open
sleepdan opened this issue Aug 26, 2022 · 1 comment
Open

Spark Java - strange behavior when uploading files #1265

sleepdan opened this issue Aug 26, 2022 · 1 comment

Comments

@sleepdan
Copy link

In my project I want to try to upload files, here is the part of the code responsible for this:

        MultipartConfigElement multipartConfigElement =
                new MultipartConfigElement(
                        "/tmp_files",
                        avatarSize,
                        avatarSize,
                        1024
                );

        request.raw().setAttribute(
                "org.eclipse.jetty.multipartConfig",
                multipartConfigElement
        );

        Part uploadedFile = request.raw().getPart("file");

And a request to upload a file using Idea's http client:

POST http://localhost:8080/users/me/avatar
Content-Type: multipart/form-data; boundary=abcd
Authorization: Bearer {{authToken}}

--abcd
Content-Disposition: form-data; name="file"; filename="test.png"

< /Users/user1/resources/test.png
--abcd--

where test.png is a regular picture.
But when I try to load in this code place:

Part uploadedFile = request.raw().getPart("file");

I get an error:

java.nio.file.NoSuchFileException: /tmp_files/MultiPart11851484240893602177
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:218)
	at java.base/java.nio.file.Files.newByteChannel(Files.java:375)
	at java.base/java.nio.file.Files.createFile(Files.java:652)

It can be assumed that this error is due to the fact that there are no write permissions to the root of the file system (I'm testing on mac os, under the user).
But if i try to upload another file - which is just a zip file then everything works.

POST http://{{host}}/users/me/avatar
Content-Type: multipart/form-data; boundary=abcd
Authorization: Bearer {{authToken}}

--abcd
Content-Disposition: form-data; name="file"; filename="file123.zip"

< /Users/18493151/develop/icandev/api-gateway/src/main/resources/file123.zip
--abcd--

and no exception in this line:

Part uploadedFile = request.raw().getPart("file");

Why is this happening? Why does the result depend on the file type?

sparkjava version 2.9.4

@ChaneWq
Copy link

ChaneWq commented Aug 29, 2022

I don't know.

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

No branches or pull requests

2 participants