java code to download a file not working..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laksh2007
    New Member
    • Dec 2007
    • 3

    #1

    java code to download a file not working..

    Hi all,
    I am new to this programing.
    can any one help me to proceed with my coding - to download a file from a remote location.

    i have writtent he following code:

    [ jsp ]

    <body>
    Click <a href="/filefunctions/DownLoadServlet ?file=C:/uploads/test1.txt">
    here</a> to download text1.txt
    </body>

    [ servlet ] :

    final ServletOutputSt ream out = response.getOut putStream();
    response.setCon tentType("appli cation/octet-stream");

    //Get the URL
    // Get the resource to view
    String file = request.getPath Info();
    System.out.prin tln("file "+file);
    if (file == null) {
    out.println("Ex tra path info was null; should be a resource to view");
    return;
    }

    // Convert the resource to a URL
    URL url = getServletConte xt().getResourc e(file);
    if (url == null) { // some servers return null if not found
    out.println("Re source " + file + " not found");
    return;
    }

    // Serve the file
    InputStream in = url.openStream( );
    byte[] buf = new byte[4 * 1024]; // 4K buffer
    int bytesRead;
    while ((bytesRead = in.read(buf)) != -1) {
    out.write(buf, 0, bytesRead);
    }
    }



    but it is printing "Extra path info was null; should be a resource to view"

    can any one help how to overcome this , and download the file from remote loaction successfully..

    please reply as early as possible , i have very less time to complete the coding

    thanks in advance

    please any one help me..its emergency


    /laksh
Working...