我想将URL重定向到我服务器上的文件,这样当打开该URL时,它会从服务器获取特定的pdf文件并在浏览器上打开它。 它适用于客户端服务器界面。 请帮我以何种方式将我的URL重定向到我服务器上的特定文件。
答案 0 :(得分:0)
new File(path).toURI().toURL();
答案 1 :(得分: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'和获取参数,但这应该可以帮助你。