on 09-14-2005 10:12 AM
Hello everyone,
I am currently writing an iView that forces the download
of a PDF file instead of opening it by default in the
users browser. I set content type and content disposition
and then try and stream the pdf. It prompts me for download,
as it should, and i can save the file. Once it's downloaded though, it can't open the file in the adobe
Reader and says the file is damaged. I tried opening
it in notepad and the contents of the file were as
follows:
"Error Streaming the Data"
I have included the two pieces of code i have tried below
anyone got any clues as to why it isn't working?
public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
{
IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
HttpServletResponse response = request.getServletResponse(true);
//Retrieve values from properties file
docURL = userProfile.getProperty("docURL");
file = request.getParameter("rid");
//set the content type(can be excel/word/powerpoint etc..)
response.setContentType ("application/pdf");
//get the file name
String name = file.substring(file.lastIndexOf("/") + 1,file.length());
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);
String ErrorStr = null;
try{
//find the right MIME type and set it as contenttype
PrintWriter outstr = new PrintWriter(response.getOutputStream());
InputStream in = null;
try{
URL url = new URL(URLEncoder.encode("http://mydomain.goes.here" + file));
URLConnection urlc= url.openConnection();
int length = urlc.getContentLength();
in = urlc.getInputStream();
response.setContentLength(length);
int ch;
while ( (ch = in.read()) != -1 ) {
outstr.print( (char)ch );
}
} catch (Exception e) {
e.printStackTrace();
ErrorStr = "Error Streaming the Data";
outstr.print(ErrorStr);
} finally {
if( in != null ) {
in.close();
}
if( outstr != null ) {
outstr.flush();
outstr.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void doOnNodeReady(IPortalComponentRequest request, IEvent event)
{
IPortalComponentProfile userProfile = request.getComponentContext().getProfile();
HttpServletResponse response = request.getServletResponse(true);
//Retrieve values from properties file
docURL = userProfile.getProperty("docURL");
file = request.getParameter("rid");
//set the content type(can be excel/word/powerpoint etc..)
response.setContentType ("application/pdf");
//get the file name
String name = file.substring(file.lastIndexOf("/") + 1,file.length());
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment; filename="" + name + """);
//OPen an input stream to the file and post the file contents thru the
//servlet output stream to the client m/c
int bit = 256;
int i = 0;
try
{
URL u = new URL("http://tvms0019.ad.bgep.co.uk" + file);
InputStream in = new FileInputStream(u.getFile());
ServletOutputStream outs = response.getOutputStream();
while ((bit) >= 0)
{
bit = in.read();
outs.write(bit);
}
outs.flush();
outs.close();
in.close();
}
catch (Exception ioe)
{
ioe.printStackTrace(System.out);
}
}
Note that i have tried both these pieces of code using
URL and File and neither work. I've tried printing
some debugging messages and the file path is correct.
Thanks,
Alessandro
i'm using a code looking like your second method.
However the part copying the input stream to the output stream can fail, since you write the -1 value to the output stream.
I'm using the following code :
InputStream is = new FileInputStream(...);
ServletOutputStream os = res.getOutputStream();
boolean ok = true;
while (ok) {
int input = is.read();
if (input == -1) {
ok = false;
} else {
os.write(input);
}
}
is.close();
os.close();
There is a blog on the subject. The only modification you have to to (and you already have made them in your code) is to change the headers to set the file as en attachement.
Regards
Guillaume
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I had solving your Problem, but I have an other Problem:
The Bug is in the the line
PrintWriter outstr = new PrintWriter(response.getOutputStream());
I become the equal Error with this line .
Replace it
PrintWriter outstr = response.getWriter();
My other Problem.
If I read the File (pdf) character-for-character from the FileInputStream and write the integer from the charater in the printWriter (for example 226(int) as byte E2). After than, I open the downloaded file and show it in the editor. But the charater is an other (for example 195(int) as byte C3).
Why that???
I become bugs by the pictures in the pdf-file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Amendment, changed the code. Now i get a 401 http error,
which essentially is an Unauthorised access error. How
do i fix this?
I am currently runnign these off the EP5 server and i
am logged in with a valid account. How come i am not
authorised to download files off my own server? however,
when i right click on a link and select "Save target" it
works fine!
Thanks,
Alessandro
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.