1

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

saplingPro
  • 20,769
  • 53
  • 137
  • 195
  • You're going in the wrong path with writing files to deploy folder. Your concrete problem has the same grounds as answered here: http://stackoverflow.com/questions/8885201/uploaded-image-only-available-after-refreshing-the-page/8889096#8889096 – BalusC Jan 28 '13 at 14:00
  • 1
    There's by the way another major mistake: the `file://` link points to the local disk file system and this will obviously not work when the enduser does not have exactly this file on its local disk file system (which may happen when the enduser with the webbrowser runs at a physically different machine than the webserver, like as would occur in real world). You need (working!) `http://` links. – BalusC Jan 28 '13 at 14:04
  • @BalusC Can you tell me how do I reach the _Shared_ directory using `http` that I created like :`(request.getServletContext().getRealPath("/") + "Shared").mkdir()`. – saplingPro Jan 28 '13 at 15:34
  • First fix the first mistake: using `getRealPath()` as file storage location. See the 1st comment. – BalusC Jan 28 '13 at 15:35
  • @BalusC There is a doubt,let me clear it. First I should create a directory `Shared` somewhere on the hard-disk and not using `getRealPath()`.Then I should add in the `context.xml` of application the path I am using to keep the files. Is that so ? – saplingPro Jan 28 '13 at 15:57
  • @BalusC I understand your answer there but I don't understand the `context` path.The point of making directories is also unclear to me. – saplingPro Jan 29 '13 at 02:02
  • @BalusC I have a problem.Can we please discuss here @ http://chat.stackoverflow.com/rooms/23608/discussion-between-balusc-and-saplingpro – saplingPro Jan 30 '13 at 04:52

0 Answers0