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

Make sure file storage directory exists before usage in DefaultPartHttpMessageReader #26790

Closed
cjdxhjj opened this issue Apr 12, 2021 · 1 comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@cjdxhjj
Copy link

cjdxhjj commented Apr 12, 2021

Affects: <Spring Framework version>
5.3.4

when i upload a file, it throw an exception

java.io.UncheckedIOException: Could not create temp file in /tmp/spring-multipart
	at org.springframework.http.codec.multipart.PartGenerator$CreateFileState.createFileState(PartGenerator.java:588)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	|_ checkpoint ⇢ com.nxin.passport.application.support.filter.FormDataParserFilter [DefaultWebFilterChain]
	|_ checkpoint ⇢ com.nxin.passport.application.support.filter.UrlRewriteFilter [DefaultWebFilterChain]
	|_ checkpoint ⇢ HTTP POST "/user/uploadPhoto" [ExceptionHandlingWebHandler]
Stack trace:
		at org.springframework.http.codec.multipart.PartGenerator$CreateFileState.createFileState(PartGenerator.java:588)
		at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
		at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1789)
		at reactor.core.publisher.MonoCacheTime.subscribeOrReturn(MonoCacheTime.java:142)
		at reactor.core.publisher.Mono.subscribe(Mono.java:4031)
		at reactor.core.publisher.MonoSubscribeOn$SubscribeOnSubscriber.run(MonoSubscribeOn.java:126)
		at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
		at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
		at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
		at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
		at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
		at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
		at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.nio.file.NoSuchFileException: /tmp/spring-multipart/8497015949572241043.multipart
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
	at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:215)
	at java.base/java.nio.file.Files.newByteChannel(Files.java:370)
	at java.base/java.nio.file.Files.createFile(Files.java:647)
	at java.base/java.nio.file.TempFileHelper.create(TempFileHelper.java:137)
	at java.base/java.nio.file.TempFileHelper.createTempFile(TempFileHelper.java:160)
	at java.base/java.nio.file.Files.createTempFile(Files.java:867)
	at org.springframework.http.codec.multipart.PartGenerator$CreateFileState.createFileState(PartGenerator.java:580)
	at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
	at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1789)
	at reactor.core.publisher.MonoCacheTime.subscribeOrReturn(MonoCacheTime.java:142)
	at reactor.core.publisher.Mono.subscribe(Mono.java:4031)
	at reactor.core.publisher.MonoSubscribeOn$SubscribeOnSubscriber.run(MonoSubscribeOn.java:126)
	at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
	at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

i found DefaultPartHttpMessageReader has a fileStorageDirectory property control the upload temp directory, it init as system temp dir, in linux, it is /tmp/spring-multipart, but os may sometimes remove the files in /tmp, when the spring-multipart is remove, then the upload action will fail, because the directory only init once at startup,

image

i have found the way to customize the path,
image

but it did not invoke in any place

image

please provide some way to config the path, so i can set a other place to store the tepm data to avoid the issure, the spring mvc config way is below

image

can you provide a similarity one in webflux?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 12, 2021
@poutsma poutsma changed the title webflux temp upload directory is not configurable Make sure file storage directory exists before usage in DefaultPartHttpMessageReader Apr 12, 2021
@poutsma
Copy link
Contributor

poutsma commented Apr 12, 2021

The field changed by setFileStorageDirectory is definitely used, even though the setter is not called anywhere. So could you please try again and see if it works for you?

You are correct that some operating systems are quite aggressive when deleting files in the temporary directory. So I did make some changes to make sure that we re-create the file storage directory if it ends up was deleted by the operating system.

@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 12, 2021
@poutsma poutsma added this to the 5.3.6 milestone Apr 12, 2021
Zoran0104 pushed a commit to Zoran0104/spring-framework that referenced this issue Aug 20, 2021
…eReader

Some operating systems delete temp files not just when booting up, but
also during operation. This commit makes sure that the
DefaultPartHttpMessageReader recreates the directory used to store
files in, if it's not there.

Closes spring-projectsgh-26790
lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
…eReader

Some operating systems delete temp files not just when booting up, but
also during operation. This commit makes sure that the
DefaultPartHttpMessageReader recreates the directory used to store
files in, if it's not there.

Closes spring-projectsgh-26790
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants