Access Denied or network error when trying to show file

4

I am developing an application Java Web App mounted on Apache Tomcat 8.0.27 . Both in the Internet Explorer browser and Google Chrome , I get errors when I try to show some file that I have deposited locally on my machine.

The possible downloads that you can make in the application are PDF or XML . I dynamically fill a data table in which the buttons have the paths to the files.

The buttons have the following routes:

C: \ PortalLeal \ some.pdf or C: \ PortalLeal \ some.xml

But when I click on any button, it sends me the following error messages:

This is the error message in Chrome:

This is the error message in Internet Explorer:

The way in which I fill the table from Java is this, where I validate if in the database the record has: Both routes (PDF, XML), only the route PDF or just XML, and based on that, I filled the table:

if(null != fac.getRutaPDF() && !fac.getRutaPDF().equalsIgnoreCase("") && null != fac.getRutaXML() && !fac.getRutaXML().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaPDF() ? "<a onClick=\"downloadURI('"+fac.getRutaPDF().replace("\", "\\")+"','"+nombreFilePDF+"');\" class=\"btn red btn-xs tooltips\"><i class=\"fa fa-file-pdf-o\"></i> PDF</a>" : "") +
            (null != fac.getRutaXML() ? "<a onClick=\"downloadURI('"+fac.getRutaXML().replace("\", "\\")+"','"+nombreFileXML+"');\"  class=\"btn green btn-xs tooltips\"><i class=\"fa fa-file-excel-o\"></i> XML</a>" : "")
        );
    } else if(null != fac.getRutaPDF() && !fac.getRutaPDF().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaPDF() ? "<a onClick=\"downloadURI('"+fac.getRutaPDF().replace("\", "\\")+"','"+nombreFilePDF+"');\" class=\"btn red btn-xs tooltips\"><i class=\"fa fa-file-pdf-o\"></i> PDF</a>" : "")
        );
    } else if(null != fac.getRutaXML() && !fac.getRutaXML().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaXML() ? "<a onClick=\"downloadURI('"+fac.getRutaXML().replace("\", "\\")+"','"+nombreFileXML+"');\"  class=\"btn green btn-xs tooltips\"><i class=\"fa fa-file-excel-o\"></i> XML</a>" : "")
        );
    }

Note: I noticed some problems when trying to show the files, so I had to apply the .replace("\", "\\") so that the route really was: C: \\ PortalLeal \\ algun.pdf

The <a> tags execute a downloadURI () javascript routine, which receives the full path ( C:\\PortalLeal\\algun.pdf ) from parameters, and the file name ( algun.pdf ).

<script>
    function downloadURI(uri, name) {
        var link = document.createElement("a");
        link.download = name;
        link.href = uri;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        delete link;
    }
</script>

(This method is the one that does the magic to show the files)

I did a test manually upload a file to C: \\ PortalLeal \\, and manually put the path in some database record and curiously sometimes, it does show the document (only in IExplorer ), and it shows it in the following way:

I do not know if the problem is giving ApacheTomcat or the web browser, for the permissions in the Windows folders.

    
asked by Elí Giacomelli 21.10.2016 в 18:17
source

3 answers

2

Thanks to @LuiggiMendoza I was able to find the solution:

First generate a Servlet for download:

<servlet>  
    <servlet-name>DownloadServlet</servlet-name>  
    <servlet-class>org.al.download.DownloadServlet</servlet-class>  
</servlet>
<servlet-mapping>  
    <servlet-name>DownloadServlet</servlet-name>  
    <url-pattern>/DownloadServlet</url-pattern>  
</servlet-mapping>

Then in my Java class that dynamically fills the command data, I call the Servlet in this way:

if(null != fac.getRutaPDF() && !fac.getRutaPDF().equalsIgnoreCase("") && null != fac.getRutaXML() && !fac.getRutaXML().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaPDF() ? "<a href=\"DownloadServlet?fileName="+nombreFilePDF+"\" class=\"btn red btn-xs tooltips\"><i class=\"fa fa-file-pdf-o\"></i> PDF</a>" : "") +
            (null != fac.getRutaXML() ? "<a href=\"DownloadServlet?fileName="+nombreFileXML+"\" class=\"btn green btn-xs tooltips\"><i class=\"fa fa-file-excel-o\"></i> XML</a>" : "")
        );
    } else if(null != fac.getRutaPDF() && !fac.getRutaPDF().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaPDF() ? "<a href=\"DownloadServlet?fileName="+nombreFilePDF+"\" class=\"btn red btn-xs tooltips\"><i class=\"fa fa-file-pdf-o\"></i> PDF</a>" : "")
        );
    } else if(null != fac.getRutaXML() && !fac.getRutaXML().equalsIgnoreCase("")){
        root.addProperty("accion",
            (null != fac.getRutaXML() ? "<a href=\"DownloadServlet?fileName="+nombreFileXML+"\" class=\"btn green btn-xs tooltips\"><i class=\"fa fa-file-excel-o\"></i> XML</a>" : "")
        );
    }

The magic is done by the ? fileName="+ filenameFilePDF +"

When clicking on automatic, it sends me to the servlet:

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class DownloadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String fileName = request.getParameter("fileName");
        String filepath = "C:\PortalLeal\";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        FileInputStream fileInputStream = new FileInputStream(filepath + fileName);

        File file = new File(filepath + fileName);
        if (file.exists()) {
            int i;
            while ((i = fileInputStream.read()) != -1) {
                out.write(i);
            }
            fileInputStream.close();
            out.close();
        } else {
            response.sendRedirect("uploadPDF.jsp"); 
        }
    }
}
    
answered by 21.10.2016 / 22:21
source
2

It does not access because the resources (PDF / XML) are in a folder that is not accessible to the web server .

All resources that must be shown / used in a web page must be reachable and serviceable by the web server. That is why the images, css files, javascript files, etc. they are usually in the same folder in which you have your HTML or PHP files, because the web server is allowed to access all the files within their child folders.

To serve those PDF and XML files, you must make them available to the web server either on a different web server, a different endpoint (within the same server), or within the folders of the web app that you show us.

    
answered by 21.10.2016 в 19:40
1

I see a problem with what you are proposing as a solution. What is the idea of a web application delivering a link to a file on your local disk (c :)? That system is not going to serve you if some more access, because it will never access the files.

In summary, Tomcat is not accessing the file, but your browser and probably there is a restriction.

In your javascript code, add a file to the URI: // thing your code starts creating the URLs like this:

(null != fac.getRutaPDF() ? "<a onClick=\"downloadURI('file://"+fac.getRutaPDF().replace("\", "\\")+"','"+nombreFilePDF+"');\" class=\"btn red btn-xs tooltips\"><i class=\"fa fa-file-pdf-o\"></i> PDF</a>" : "")

Likewise, in my opinion doing so is incorrect because of what I indicated earlier.

    
answered by 21.10.2016 в 19:22