Open a Local file from JSP

Hi All,

I want to access a local file from a JSP. On click of the link the local file should open. The location of the local file is

O:/VWS00000000000000000001/REPORT_FRAGMENTS/Title1.doc

and the hyper link on the JSP shows

file:///O:/VWS00000000000000000001/REPORT_FRAGMENTS/Title1.doc

somehow the file does not open from the JSP page but it opens from the browser if I type

'file:///O:/VWS00000000000000000001/REPORT_FRAGMENTS/Title1.doc' in the address bar.

Can anybody please help.

regards,

Shardul.

[567 byte] By [shardulba] at [2007-11-27 11:03:58]
# 1

The browser has all kinds of security protection from a web site you visit accessing the end user's hard drive. In this case, your JSP page is an external web site accessing a computer via its browser. There you cant directly access the user's hard drive.

Look up the html tag <input type="file" on how to properly allow an end user to download a file from his computer.>

George123a at 2007-7-29 12:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

if you'd like to show the real path to the user, use simply an ftp server !

however, if you prefer a secure solution, so use a servlet:

example:

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class SendWord extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//get the 'file' parameter

String fileName = (String) request.getParameter("file");

if (fileName == null || fileName.equals(""))

throw new ServletException(

"Invalid or non-existent file parameter in SendWord servlet.");

// add the .doc suffix if it doesn't already exist

if (fileName.indexOf(".doc") == -1)

fileName = fileName + ".doc";

String wordDir = getServletContext().getInitParameter("word-dir");

if (wordDir == null || wordDir.equals(""))

throw new ServletException(

"Invalid or non-existent wordDir context-param.");

ServletOutputStream stream = null;

BufferedInputStream buf = null;

try {

stream = response.getOutputStream();

File doc = new File(wordDir + "/" + fileName);

response.setContentType("application/msword");

response.addHeader("Content-Disposition", "attachment; filename="

+ fileName);

response.setContentLength((int) doc.length());

FileInputStream input = new FileInputStream(doc);

buf = new BufferedInputStream(input);

int readBytes = 0;

while ((readBytes = buf.read()) != -1)

stream.write(readBytes);

} catch (IOException ioe) {

throw new ServletException(ioe.getMessage());

} finally {

if (stream != null)

stream.close();

if (buf != null)

buf.close();

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

hope that helps

java_2006a at 2007-7-29 12:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I might be wrong here but you are trying to open a file on the clients machine. JSP is compiled and run on the web server. the web server cannot see the file on the clients machine.

to do this I think you need to use JScript and an ActiveX control.

Crispsa at 2007-7-29 12:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Not sure whether this can be done in JavaScript given that it runs on the client machine, try the location = command. Not sure whether the browser security would prevent it as mentioned above.

If Javascript doesn't work you have to use a file upload field, and use an inputstream to send the file to the server and open it from there. You may be able to use a hidden file upload field and set it's value using Javascript onclick of a link, then process a file from a default local drive/directory and return the file to the browser all in one process but I've not tried it.

StrutFoola at 2007-7-29 12:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...