Good morning, I have a very simple program which downloads a text file from my server c drive c:\mydir\test.txt to a user's computer specific drive. However, the program constantly giving me error telling me that "The system cannot find the path specified". I don't know what is going wrong in the program and why the
servlet could not find the specific file test.txt in my c drive c:\mydir\test.txt. Attach is my program for you to review.
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.BufferedInputStream" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.IOException" %>
<%
// you can get your base and parent from the database
String filename="test.txt";
String filepathServer = "c:\\mydir\\";
BufferedInputStream buf=null;
ServletOutputStream myOut=null;
try{
myOut = response.getOutputStream( );
File myfile = new File(filepathServer+filename);
out.println ("Server file " + myfile + "\n");
//set response headers
response.setContentType("text/plain");
response.addHeader(
"Content-Disposition","attachment; filename="+ filename );
response.setContentLength( (int) myfile.length( ) );
FileInputStream input = new FileInputStream(myfile);
buf = new BufferedInputStream(input);
int readBytes = 0;
//read from the file; write to the ServletOutputStream
while((readBytes = buf.read( )) != -1)
myOut.write(readBytes);
} catch (IOException ioe){
throw new ServletException(ioe.getMessage( ));
} finally {
//close the input/output streams
if (myOut != null)
myOut.close( );
if (buf != null)
buf.close( );
}
%>