我想将 URL 重定向到我服务器上的文件,这样当打开该 URL 时,它会从服务器获取特定的 pdf 文件并在浏览器上打开它。它适用于客户端服务器接口。请帮助我如何将我的 URL 重定向到我服务器上的特定文件。
问问题
347 次
2 回答
0
如果我正确理解您的问题,假设您有以下网址:
http://somesiste.bla/render?path=/dirx/hello1.pdf
如果 PDF 文件位于您的应用程序 WAR 文件中,并且您的 WAR 文件看起来像这样
WAR
-- index.jspx
-- WEB-INF
--web.xml
-- data
--dirx
--hello01.pdf
--hello02.pdf
那么这真的很容易,只需转发到您的应用程序中的正确文件
public void forwardToPdf( HttpServletRequest request, HttpServletResponse response, String path ) throws ServletException, IOException
{
RequestDispatcher requestDispatcher= request.getRequestDispatcher("/data/" +path) ;
requestDispatcher.forward( request, response ) ;
}
// Get the parameter and pass it on
String path = getParameter("path");
forwardToPdf(request, response, path);
但是如果文件位于您的应用程序之外,可以说
C:\data\
您不能只重定向到该文件,您要做的就是读入该文件并将其呈现给用户
只是一个小实用程序来做到这一点。
import java.io.Closeable;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
public class FileDownloadHelper {
public static final String CONTENT_TYPE_EXCEL ="application/vnd.ms-excel";
public static final String CONTENT_TYPE_WORD_DOCUMENT ="application/doc";
public static final String CONTENT_TYPE_MS_WORD ="application/msword";
public static final String CONTENT_TYPE_PDF ="application/pdf";
public static final String CONTENT_DISPOSITION_INLINE ="inline";
public static final String CONTENT_DISPOSITION_ATTACHMENT ="attachment";
public void write(HttpServletResponse response, byte[] data, String contentType, String outputFileName, String contentDisposition){
response.setCharacterEncoding("UTF-8");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
response.setStatus(HttpServletResponse.SC_OK);
if(data != null){
response.setContentType(contentType);
long contentLength = data.length;
/* IE requires the following for pdf files */
response.setBufferSize((int)contentLength);
//This enables us to show estimated download time
response.setHeader("Content-Length", String.valueOf(contentLength));
// inline=forces to use a viewer attachment=show save dialog
//response.setHeader("Content-Disposition", "inline; filename=\"" + outputFileName + "\"");
response.setHeader("Content-Disposition", contentDisposition+"; filename=\"" + outputFileName + "\"");
// These set of headers need to be here for this to work with IE with servlet security
// This will prevent catching of the results
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
sos.write(data);
}else{
sendResponse404(response);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
close(sos);
}
}
/**
* Helper method to send 404 - Resource not found message
* @param response
*/
private void sendResponse404(HttpServletResponse response) {
//Resource not found
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.setContentType("text/html");
try {
response.getOutputStream().write("Resource not found".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Helper method to take care of cleanup
* @param resource to close
*/
private void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
然后你可以简单地使用它如下
// Force browser to download the resource
FileDownloadHelper download = new FileDownloadHelper();
download.write(response, readFileToBytes(pathToFileOnYourFileSystem), FileDownloadHelper.CONTENT_TYPE_PDF,"OUTPUTFILENA.PDF",
FileDownloadHelper.CONTENT_DISPOSITION_ATTACHMENT
我希望这会有所帮助并且有意义。缺少一些东西,例如“readFileToBytes”和获取参数,但这应该可以帮助您。
于 2012-11-29T13:04:46.340 回答
0
new File(path).toURI().toURL();
于 2012-11-29T11:06:16.187 回答