Link doesn't work when I access it via localhost

老子叫甜甜 提交于 2019-12-13 19:26:16

问题


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 ?

(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.

and

来源:https://stackoverflow.com/questions/14563338/link-doesnt-work-when-i-access-it-via-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!