首页 > 解决方案 > 如何在tomcat服务器端访问文档

问题描述

在我的项目中,我有一个 .pdf、.docx 等。我的 tomcat 服务器端的文档。我想访问它们,并且想将它们复制到我的本地计算机驱动器中。但我每次都会收到这个错误。

java.io.FileNotFoundException: \\ptrisf02\group2\Engine_Follow\V2500-A5\V2500-A5_e-Archive\EV11929-02\MiniPack\GEN\EV11929-02 ACCESSORY AND EBU CHECK LIST EEV. 01.pdf (Access is denied)
    java.io.FileInputStream.open0(Native Method)
    java.io.FileInputStream.open(Unknown Source)
    java.io.FileInputStream.<init>(Unknown Source)
    java.io.FileInputStream.<init>(Unknown Source)
    org.solr.openDocumentServlet.doPost(openDocumentServlet.java:78)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

我试图更改catalina.policy以授予权限。但它没有用。我怎么解决这个问题??这是我添加到的代码片段catalina.policy

//These permissions apply to the E-Archive
grant codeBase "file:${catalina.home}/webapps/E-Archive"{
        permission java.io.FilePermission
          "${\\\\ptrisf02\\group2\\Engine_Follow}","read";
};

我的代码是这样的

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String Docpath=request.getParameter("document");
        String docname=request.getParameter("docName");
        PrintWriter pw=new PrintWriter(System.out);
        pw.write(Docpath);
        String r_Docpath=Docpath.replace("\"", "\\");
        String r2_Docpath=r_Docpath.replace("\\", "\\\\");
        //response.getWriter().append(r2_Docpath);
        File original=new File(r2_Docpath);
        File destination=new File("D:\\");


        try {
            //Files.copy(original.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
            FileUtils.copyFileToDirectory(original, destination);

        }catch(IOException e){
            e.printStackTrace();
        }
        File file=new File("D:\\"+docname);
        Path filePath=Paths.get("D:\\"+docname);
        response.setContentType(Files.probeContentType(filePath));
        response.setHeader("Content-Disposition", "inline;filename="+docname);

        InputStream input=new FileInputStream(Docpath);

        //FileOutputStream out=new FileOutputStream(Docpath);

        ServletOutputStream  outs =  response.getOutputStream();
        BufferedInputStream bis=new BufferedInputStream(input);
        BufferedOutputStream bos=new BufferedOutputStream(outs);
        byte[] buf = new byte[4096]; 
        int bytesRead;
        while ((bytesRead = bis.read(buf)) > 0) {
            bos.write(buf, 0, bytesRead);

        }
        bis.close();
        bos.close();


        //doGet(request, response);
    }

标签: javatomcat

解决方案


推荐阅读