2012-11-29 100 views
0

我想将URL重定向到我的服务器上的文件,以便当打开该URL时,它会从服务器获取特定的pdf文件并在浏览器中打开它。 它在客户端服务器接口上工作。 请帮我用什么方式将我的URL重定向到我的服务器上的特定文件。将本地URL映射到本地系统上的文件

+0

你可以试试''如果你使用的是spring mvc – white

回答

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'和获取参数,但这应该让你去。

相关问题