Java 将URL映射到loca lsystem上的文件

Java 将URL映射到loca lsystem上的文件,java,spring,servlets,mapping,Java,Spring,Servlets,Mapping,我想将URL重定向到服务器上的文件,这样当打开该URL时,它会从服务器获取特定的pdf文件并在浏览器上打开它。 它在客户机-服务器界面上工作。 请帮助我如何将URL重定向到服务器上的特定文件。如果我正确理解您的问题,假设您有以下URL: new File(path).toURI().toURL(); http://somesiste.bla/render?path=/dirx/hello1.pdf 如果PDF文件位于您的应用程序WAR文件中,并且您的WAR文件如下所示 WAR -- ind

我想将URL重定向到服务器上的文件,这样当打开该URL时,它会从服务器获取特定的pdf文件并在浏览器上打开它。 它在客户机-服务器界面上工作。
请帮助我如何将URL重定向到服务器上的特定文件。

如果我正确理解您的问题,假设您有以下URL:

new File(path).toURI().toURL();
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”和获取参数,但这应该可以让您继续。

如果您使用的是spring mvc,您可以尝试