Tomcat 9.0.6无法提供应用目录内静态PDF文件问题求助
Hey there, let's troubleshoot why your PDF link isn't working in your Tomcat setup. Based on your environment (Ubuntu 16.04, Tomcat 9.0.6, Java 8) and directory structure, here are the most common issues and fixes:
The first thing to check is your PDF link in the JSP pages. Spaces in filenames can cause encoding issues in browsers.
- Option 1: Rename
User Doc.pdftoUser_Doc.pdforUserDoc.pdf(no spaces), then update your link to match, like:<a href="userDocs/User_Doc.pdf">View User Document</a> - Option 2: If you keep the space, encode it as
%20in your link:<a href="userDocs/User%20Doc.pdf">View User Document</a>
You can test this directly by typing the full URL into your browser (e.g., http://your-server:8080/RimWeighting/userDocs/User%20Doc.pdf). If this loads the PDF, your page's link path was the problem.
Tomcat runs under a dedicated user (usually tomcat or www-data on Ubuntu), so it needs read access to your userDocs directory and the PDF file. Run these commands to fix permissions:
# Navigate to your app's directory cd /var/lib/tomcat9/webapps/RimWeighting # Give read/execute access to the directory sudo chmod -R 755 userDocs/ # Give read access to the PDF file sudo chmod 644 userDocs/User\ Doc.pdf # Set the owner to Tomcat's user (adjust if your user is different) sudo chown -R tomcat:tomcat userDocs/
If your WEB-INF/web.xml has a <security-constraint> configuration, it might be blocking access to the userDocs directory. Look for rules that restrict GET requests to static resources, and add an exception for userDocs/* if needed.
Tomcat should handle PDFs by default, but if the MIME type is missing, browsers won't render the file correctly. Check your Tomcat conf/web.xml for this entry:
<mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type> </mime-mapping>
If it's missing, add it and restart Tomcat.
- Directly access the PDF via full URL in your browser. If it works, fix your page's link.
- If not, check file permissions first (Ubuntu often locks down file access).
- Finally, check web.xml for security rules and MIME type settings.
That should get your PDF loading in the browser!
内容的提问来源于stack exchange,提问作者Robert P.

