簡単な答え:これらのファイルをどこから取得するかを知っているサーブレットを作成し、それを提供させます。
より長いバージョン/説明:
サーブレットを作成し、次のようにマップします/yourapp/pdfs/*
。
@WebServlet("/pdfs/*")
public class PdfServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) {
String basePath = getServletContext().getInitParameter("basePath");
File f = new File(basePath + File.separator + req.getPathInfo());
/////////////////////////////////////////////////////////////////////
//// BIG WARNING: ////
//// Normalize the path of the file and check if the user can ////
//// legitimately access it, or you created a BIG security ////
//// hole! If possible enhance it with system-level security, ////
//// i.e. the user running the application server can access ////
//// files only in the basePath and application server dirs. ////
/////////////////////////////////////////////////////////////////////
if( f.exists() ) {
OutputStream out = res.getOutputStream();
// also set response headers for correct content type
// or even cache headers, if you so desire
byte[] buf = new byte[1024];
int r;
try( FileInputStream fis = new FileInputStream(f) ) {
while( (r=fis.read(buf)) >= 0 ) {
out.write(buf, 0, r);
}
}
}
else res.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
上記のコードは微調整されている可能性がありますが、一般的には解決策の概要を説明しています...
で context init パラメータを指定しますweb.xml
。
<context-param>
<param-name>basePath</param-name>
<param-value>/the/path/to/the/pdfs</param-value>
</context-param>