File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes error in dwonloading a pdf file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "error in dwonloading a pdf file" Watch "error in dwonloading a pdf file" New topic
Author

error in dwonloading a pdf file

Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
hello ranchers,
Posted below is my code to download a file from server.This workign fine for .doc ,outlook files.But the same fails to work for .pdf
I m getting Adobe Reader could not open file because it is either not a supported file type or because file has been damaged"



Any help will be appreciated.Please treat request on high priority...
Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
The code for PDF does not set any of the response headers; in particular it's missing content length and content type.

Why don't you use the same code for downloading ALL files types? The only differences should be in content type and maybe content disposition, no? That'd be easier to maintain.


Ping & DNS - my free Android networking tools app
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
even if i use same code for all downloads,i m getting the same error.....
setting the content length and content type doesnot help..still same error exist
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
A further problem is that the PDF code writes the full array in each loop iteration - it should only write as many bytes as it has read (like the JPEG code does).
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
i didnt get you..can you please be more precise...a piece of code would be helpful
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
Compare

from the JPEG code with

from the PDF code. Notice the difference?
Ajith Anand
Ranch Hand

Joined: Aug 30, 2004
Posts: 40
In addition to what Ulf mentioned, I also see that fos.close() is also missing in your code.....


LXI Technologies P Ltd
[url]www.lxisoft.com[/url]
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
below is what error i get..once i save the downloaded pdf file .
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
can any one tell me how can i paste an image......
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
Have you fixed your code to deal with the above-mentioned problems?

In order to show images you need to upload them somewhere where they can be accessed over HTTP.
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
no issues is yet not resolved.still not able open my pdf document.
Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
Give your Email Id I Will give complete program uploading and downloading of all types of files
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
Add the following code in the PDF if block and try:


Also let us know once if you get a solution. Let us all know how you resolved it.


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
Ankita Jha
Ranch Hand

Joined: Sep 15, 2008
Posts: 55
Rohitash sharma :My gmail is swatipriyadarsani@gmail.com

Thirugnanam Saravanan : Adding contentType and setHeader didnt help.
I will defininately let you people...once my problem is resolved.
Rohitash sharma
Greenhorn

Joined: Dec 03, 2008
Posts: 19
I think i should display code in forum So it would be helpful
for beginners also

It has two files
(1) uploadtest.jsp

its coding is as follows

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<form method="post" action="fileUpload.jsp" name="upform"
enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1"
align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="uploadfile" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>


(2) file is fileupload.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.*,java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload JSP Page</title>
</head>
<body>
<h1>Upload file details JSP Page</h1>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (byteRead!=-1)
{
byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());

saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos,os;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
os = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile="C:\\temp/"+saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>
<body>
</html>


Note that i have saved file in my desired folder called 'temp' in c: drive. You may saved file anywhere
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error in dwonloading a pdf file