我找到了一个相当简单的解决方案来解决这个问题。主要部分是与 /fileDownload/* 的 url 映射
@WebServlet(urlPatterns = { "/fileDownload/*" })
@Slf4j
public class FileDownloadServlet extends HttpServlet
{
@Inject
private SimpleDocumentStoreService documentService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
String id = req.getParameter("id");
if (StringUtils.isNotEmpty(id))
{
try
{
documentService.streamDatei(id, resp);
resp.flushBuffer();
} catch (IOException e)
{
log.error("error downloading file", e);
}
}
}
}
和来电者:
<a href="#{request.contextPath}/fileDownload/#{document.name}?id=#{document.id}" target="_blank">
<h:outputText value="#{document.name}"/>
</a>