• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display text file in server from client  RSS feed

 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am having a problem with writing and displaying text file from servlet. I can display the file when developing on 1 pc but when i make the development pc as server and let other pc to access it as client, instead of the client, the server display the file. Below is part of the codes
executeTrxRpt();
Runtime rt = Runtime.getRuntime();
Process p = null;
try
{
p = rt.exec("C:\\Program Files\\Accessories\\WORDPAD.EXE C:\\ExportFileTrx.txt");
}
catch(Exception ex){}
How can I display a text file that been generated on the server from a client?
Thanks
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider what you're doing for a moment.
You're telling the machine on which the server is running to start wordpad with the file you want to show.
Of course that server will start wordpad on the machine it is running on itself, should be no surprise.
Worse, if your server is ever deployed on a machine running another operating system or on which wordpad is not installed you're in trouble.
You CANNOT do this kind of stuff in a servlet. You CAN NOT EVER start a program on the client computer from a servlet.
You SHOULD NEVER assume your servlet will run on a machine running any specific operating system (unless you're going to write it for a specific customer with very specific wishes).
 
michael yue
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i realised my mistake. Actually I want to display a file from server. Below is my new code but somehow i am unable to prompt it to download the file. Any experts please help
String pathspt = getServletContext().getRealPath("/WEB-INF/classes/max/Export.txt");
executeRpt(mnum,onum,pathspt);
try
{
URL url = getServletContext().getResource(pathspt);
if (url != null) {
URLConnection con = url.openConnection();
res.setContentType(con.getContentType());
res.setContentType("application/x-download");
//System.out.println(con.getContentType());
res.setContentLength(con.getContentLength());
//System.out.println(con.getContentLength());
res.setHeader("Content-Disposition", "attachment; filename=" + pathspt);
//System.out.println(getFileName(url));

ServletOutputStream out = res.getOutputStream();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
byte[] buf = new byte[5120];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf);
}
int i;
in.close();
out.flush();
out.close();
}
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard of a header "attachment".
You'd be better off creating a servlet that pipes the content of the file into its outputstream and creating a url that calls that servlet with some identifier for the file (do not use the full path and filename for security reasons or people could tell it to pipe for example the password file).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!