我想将URL重定向到我的服务器上的文件,以便当打开该URL时,它会从服务器获取特定的pdf文件并在浏览器中打开它。 它在客户端服务器接口上工作。 请帮我用什么方式将我的URL重定向到我的服务器上的特定文件。将本地URL映射到本地系统上的文件
0
A
回答
0
new File(path).toURI().toURL();
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'和获取参数,但这应该让你去。
相关问题
- 1. 虚拟文件系统映射到本地文件
- 2. 文件系统API - 从本地驱动器上传到本地文件系统
- 3. 如何使本地文件系统的映射写在Hadoop中
- 4. Spotify API将URL重定向到本地文件系统?
- 5. 本地文件系统上的Hadoop
- 6. jsdom本地文件系统
- 7. 将json写入本地文件系统
- 8. VIM - 将本地文件夹映射到远程文件夹
- 9. s3文件到本地系统php
- 10. 将GWT文件上传到本地系统
- 11. 如何将文件从本地文件系统复制到HDFS文件系统?
- 12. 本地主机到文件系统的绝对url路径
- 13. 无法创建本地文件系统如何写入本地文件系统
- 14. 将URL映射到Tomcat 7中的文件系统
- 15. JS文件系统API:访问本地系统上的JS FileEntry?
- 16. Wicket:资源URL映射(文件系统)
- 17. 本地文件系统中的PDF文件/ URL
- 18. 如何将文件从HDFS复制到本地文件系统
- 19. 将UNC文件系统目标位置发布到本地文件系统
- 20. 如何在Ruby中将URL映射到文件系统?
- 21. 我可以将SMS存储映射到本地文件夹吗?
- 22. 将绝对/相对(本地)路径映射到绝对URL
- 23. Git将本地系统信息库推送到本地系统信息库(Windows)
- 24. 简单的方式反映一个本地文件系统
- 25. 使用Python脚本将文件夹从本地系统上传到FTP
- 26. 跨浏览器链接到本地系统上的文件
- 27. 需要替代readarray脚本/映射文件旧系统上
- 28. 将地图任务输出写入本地文件系统?
- 29. 将Spark工作写入本地文件系统还是从本地文件系统读取?
- 30. TFBuild访问本地文件系统
你可以试试' '如果你使用的是spring mvc –
white