Internal Magic of a multipart file upload Springboot - spring

I have spend lots of time to understand the internal working of a multipart file upload in spring boot.
Couldn't get a clear picture on it.
Bit confused about the role of spring boot tmp directory.
I have a tmp directory named as /tmp/tomcat.4296537502689403143.8587/work/Tomcat/localhost/ROOT]
I was checked the tmp directory during the file upload, couldn't write anything here.If i delete the folder it will throw multipart error.
Can anyone explain the internal working of a file upload and the role of tmp directory.

Spring boot web framework comes with embedded web servers: Tomcat by default. Tomcat creates/uses the tmp directory to store temporary files; including uploaded files, session files, and other files.
That behavior can be changed through configuration. Alternatively, you can also configure spring boot to use a different web server.
https://github.com/spring-projects/spring-boot/blob/70eee612ff2a2b1e58cbcb18a4d46e464895c18a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

Related

Where is the work directory of embedded tomcat for a spring boot / spring mvc application?

I would like to ask where is the location where you can see the work directory of a spring boot / spring mvc applicaiton. Specifically I want to know Transpiled JSP files converted to java files.
I can see the class files inside the target directory, but cannot find the transpiled jsp files. Is there a class call where I can see it in my environment? Like System.getProperty("user.dir") or new java.io.File(".").getCanonicalPath(). I tried both and it only showed me the code path.
Also, I know the Eclipse Server path. But in spring boot, it does not seem to run using a server instance of tomcat. So the following path is not existing?
The following directory is also blank.
projectworkspace/.metadata/.plugins/org.eclipse.wst.server.core/
The base dir is set via the property server.tomcat.basedir, see: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
The work dir is derrived from there by appending /work.

Tomcat Servlet 3.0 multipart file upload name clash in temp folder

It appears Tomcat is not handling parallel uploads of the same multipart file by two different users.
Test
Two sessions/users A & B
Both upload a 20MB file with the same name foo.pdf more or less at the same time
Servlet 3.0 Request with default configuration stores the two files in the tmp folder
Both threads try to write that foo.pdf into the tmp folder
Result
The uploaded document is corrupt (two streams writing to it)
The slower request will fail with a FileNotFoundException as the tmp file was already deleted by the cleanup task of the faster request.
Is there a way to avoid this - other than setting fileSizeThreshold higher than maxFileSize so it would never be written to disk in the first place.
Side note: this is a Spring Boot 2.1 application but this is irrelevant as it uses this Servlet 3.0 implementation by default.
I have an answer but it's not really satisfactory. We didn't figure out how to make this work with Tomcat's Servlet 3.0 implementation. However, once we switched to Apache commons-fileupload all was well.
So, for Spring (Boot) applications you would
set spring.servlet.multipart.enabled: false
configure a bean of type CommonsMultipartResolver with a name of multipartResolver
add the commons-fileupload dependency

Get all config files with spring cloud config

I am relatively new to spring cloud config and I am able to pull the config file with the application name. There is a need for me to pull all the config files from Git via Spring Cloud Config without having to know or pass the application name in the context.
Currently I have used "spring.cloud.config.server.git.searchPaths= *" in my bootstrap file to search my entire git project and using "/{application}-{profile}.properties" to pull the properties files matching the application file. Would want to pull all properties files without passing this application name, is this possible?
I would require this logic to know how many properties files are checked in and how many are duplicates and this logic will be handled by my custom REST service that I am planning to write.

Spring boot 404 error if the static content is deployed as jar

I have spring boot application and angular js project as two different modules.
The angular js contents ( files inside 'dist' folder) are converted as jar files and included as part of Spring boot application. The folder structure inside the jar is /static/**.
The jar appears inside the lib folder of the spring boot application.
Now when i try to access the application http://localhost:8080, i get 404 error.
It works fine, if i copy the "dist" folder content inside the /resources/static and create spring boot jar file.
Am i missing anything when the static contents are included as jar file. ?
Thanks in advance
I have a similar set up like you but using Angular 4. My Spring Boot version is 1.5.3.RELEASE.
My build is based on Maven with two modules. One for front-end (JAR) and one for backend (WAR), which references first module with Maven dependency mechanics.
Providing static content works out of the box. Since I'm using Spring Security I have to configure access rules for this resources. But this issue should be answered with a 401 and not 404.
Your angular JAR structure seems to be correct. Is your front-end build complete?
The lib folder with the front-end JAR is in my Spring Boot WAR on path /WEB-INF/.
Did you check if there is a bug connected with your used Spring Boot version?
#Moshe Arad #Raj In my understanding from https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot ,
the static contents can be in one of those mentioned locations. However, I myself is facing struggle in serving static content. I was working on spring-boot and angular-cli(6.1.5) based app. For deploy purpose, so that UI works on spring-boot port; I ran ng build. It generates files in src/main/resource/static (changed outputPath in angular.json to resources/static and create an empty dir static in resources) . But spring-boot didn't serve any angular content at 8080. Although index.html has <base href="/">, I thought to be more specific for it to pick/serve .js contents from static dir so I ran :
ng build --base-href / --deploy-url /static/ . This time the generated index.html has /static/runtime.js etc. in . But when accessing localhost:8080/ (right click -> inspect shows 200 response code) still doesn't show any UI content. But hitting localhost:8080/static/runtime.js shows 404 .
I believe there is more to do beyond just adding content to resources/static as probably tomcat is trying to serve it but somehow the content not being displayed. Because I do notice favicon icon (angular icon) on browser tab and the contents of jar file shows generated static files in the path (BOOT-INF/classes/static/index.html etc.)

Clear web pages cache in jboss

I develop a web app:
frontend javascript
transport json
backend Java EE
JBoss AS 6.1
All static html and js files are in an exploded .war directory which itself is in an exploded .ear directory.
Normally everything works fine! Today I changed a html file and copied it to the .war directory.
When the browser loaded the file it was the old one. So I started wireshark and saw in wireshark the get request and the returned OLD file, even when I changed the filename in the .war directory.
So there is some caching in jboss. I started Googling and found some posts about the tmp and work folders.
I looked in my "...\server\default\work..." folders, but everything was empty.
In "...\server\default\tmp" I found some files but no one seems to be related to my cached page.
As this is a tomcat-in-jboss issue, you have to go to the tomcat area in jboss.
In my case ..\server\default\deploy\jbossweb.sar. In this directory you should find a file called context.xml.
Now stop jboss.
In the context tag there are 2 attributes - cookies and crossContext. Now just add the attribute cachingAllowed="false" and start jboss again:
<Context cookies="true" crossContext="true" cachingAllowed="false">
If you need only one time solution, just create fresh ear with fixed HTML file, undeploy the old one and deploy the new one.

Resources