Link doesn't work when I access it via localho

2019-08-04 03:30发布

The following servlet creates a directory named Shared and then copies the video into this directory.Next it presents the link,to download this video. But when I click the link,nothing happens. Why is this ?

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    String path = request.getServletContext().getRealPath("/") + "Shared/" + "sweet-love-story-that-might-make-your-day[www.savevid.com].3gp";
    path = path.replace("\\","/");
    try {
        File f = new File(request.getServletContext().getRealPath("/") + "Shared/");
        if(!f.exists()) {
            f.mkdir();
            // now copy the animation to this directory
        } else {
            System.out.println("directory already made");               
        }
        writer.println("<html> <head> <title> </title> </head>");
        writer.println("<body>");
        writer.println("<a href=\"file:///" + path + "\"" + ">Click to download</a>");
        writer.println("</body>");
        writer.println("</html>");
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

Ironically when I write a html that is in the same directory as the video (in Shared) I am able to download/see the video. Why doesn't the link work when I access it via localhost ?

enter image description here

(I am using Tomcat)

**Note: The statement request.getServletContext().getRealPath("/") prints W:\UnderTest\NetbeansCurrent\App-1\build\web**

The following are the snapshots of html accessed from localhost and locally respectively.

enter image description here

and

enter image description here

0条回答
登录 后发表回答