When dealing with file uploads in Tomcat on Windows Server, many administrators face a critical storage management issue. The default temporary directory location (java.io.tmpdir
) typically points to system drive locations like C:\Windows\Temp
or similar paths with limited space allocation.
During file upload processing:
- Tomcat temporarily stores the entire uploaded file in the temp directory
- Even if your application moves files to NAS after upload
- The temporary copy remains until garbage collection
- Large uploads can exhaust available disk space
There are multiple ways to change the temp directory location, but the most effective method depends on your Tomcat deployment:
Method 1: JVM System Property
For Windows services using "Monitor Tomcat" or similar management tools:
# Add this to your Java Options in Tomcat configuration:
-Djava.io.tmpdir=D:\\your\\custom\\temp\\path
Method 2: Environment Variable
If running Tomcat from command line or startup scripts:
SET CATALINA_TMPDIR=D:\your\custom\temp\path
Method 3: Service Wrapper Configuration
For Tomcat instances running as Windows services:
# In your service configuration (wrapper.conf or similar):
wrapper.java.additional.1=-Djava.io.tmpdir=D:\\your\\custom\\temp\\path
After making changes, verify the new temp directory is being used:
- Create a simple JSP page with this code:
<%= System.getProperty("java.io.tmpdir") %>
- Restart Tomcat completely
- Check the output matches your configured path
- Ensure the new directory has proper read/write permissions for Tomcat service account
- Monitor disk space usage in the new location
- Consider implementing periodic cleanup of old temp files
- For clustered environments, ensure consistent temp paths across nodes
When handling file uploads in Tomcat, the server stores temporary files in the system's default temp directory before processing. This becomes problematic when:
- The system drive has limited space
- You need better performance from a dedicated storage volume
- Security policies require specific storage locations
On Windows Server 2003 (and newer versions), the default location is typically C:\Windows\Temp
, which is often unsuitable for production environments handling large file uploads.
There are three primary ways to configure Tomcat's temp directory:
1. Environment Variable Approach
First attempt (often overlooked by Windows services):
# Set system environment variable
set CATALINA_TMPDIR=D:\app_temp\tomcat
This may not work if Tomcat runs as a service because Windows services often don't inherit user environment variables.
2. JVM Parameter Method (Recommended)
The most reliable approach is setting the JVM parameter either in:
- Tomcat service configuration (for Windows services)
- Catalina startup scripts (for manual startup)
For Tomcat Windows Service:
# Using Tomcat Monitor GUI:
1. Open "Monitor Tomcat" application
2. Navigate to Java > Java Options
3. Add or modify: -Djava.io.tmpdir=D:\\app_temp\\tomcat
4. Restart Tomcat service
For startup scripts (catalina.bat/catalina.sh):
# Add to catalina.bat (Windows)
set "JAVA_OPTS=%JAVA_OPTS% -Djava.io.tmpdir=D:\app_temp\tomcat"
# For catalina.sh (Linux/Mac)
JAVA_OPTS="$JAVA_OPTS -Djava.io.tmpdir=/opt/app_temp/tomcat"
3. Context Configuration
For web application-specific configuration (less common):
<Context>
<Parameter name="javax.servlet.context.tempdir"
value="D:\app_temp\myapp"
override="false"/>
</Context>
After configuration, verify the setting:
// Create a simple JSP page (tempdir_test.jsp)
<%@ page import="java.io.File" %>
<%
File tempDir = (File) application.getAttribute("javax.servlet.context.tempdir");
out.println("Temp directory: " + tempDir.getAbsolutePath());
%>
Common issues to check:
- Ensure the directory exists and Tomcat has write permissions
- Watch for path separator differences (Windows uses backslashes but requires double backslashes in JVM parameters)
- Check for conflicting configurations in multiple locations
For enterprise deployments:
- Create dedicated directories per application
- Set appropriate disk quotas
- Consider using RAM disks for high-performance requirements
- Implement cleanup routines for orphaned temp files
// Example cleanup filter for file uploads
public class TempCleanupFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
chain.doFilter(request, response);
} finally {
Part part = request.getPart("file");
if (part != null) {
part.delete(); // Clean up temp file
}
}
}
}