Java equivalent to these 5 lines of Perl

P

Page

This has got to be the easiest question ever asked on this forum, but
since I have very little Java experience and can not seem to find
anything about this, I would appreciate if someone could take two
minutes and tell me what Java code I would need to get a servlet to
perform the following Perl code:

open (MYFILE, "filename.pdf") || die;
binmode STDOUT; # this line needed for win32 machines but not needed
on Unix
binmode MYFILE; # this line needed for win32 machines but not needed
on Unix
print <MYFILE>;
close (MYFILE);
 
M

Michael Borgwardt

Page said:
This has got to be the easiest question ever asked on this forum, but
since I have very little Java experience and can not seem to find
anything about this, I would appreciate if someone could take two
minutes and tell me what Java code I would need to get a servlet to
perform the following Perl code:

open (MYFILE, "filename.pdf") || die;
binmode STDOUT; # this line needed for win32 machines but not needed
on Unix
binmode MYFILE; # this line needed for win32 machines but not needed
on Unix
print <MYFILE>;
close (MYFILE);

untested:

InputStream in = new FileInputStream("filename.pdf");
byte[] buffer = new byte[4096];
int index;
while((index = in.read(buffer)) != -1){
response.getOutputStream().write(buffer, 0, index);
}
in.close();

Has the added benefit that the servlet container will probably give you
a nice error page (or at least a detailed log entry) if something goes
wrong, e.g. the file doesn't exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top