File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error in dwonloading a pdf file

 
Ankita Jha
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankita Jha
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt get you..can you please be more precise...a piece of code would be helpful
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare

from the JPEG code with

from the PDF code. Notice the difference?
 
Ajith Anand
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to what Ulf mentioned, I also see that fos.close() is also missing in your code.....
 
Ankita Jha
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below is what error i get..once i save the downloaded pdf file .
 
Ankita Jha
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one tell me how can i paste an image......
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no issues is yet not resolved.still not able open my pdf document.
 
Rohitash sharma
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give your Email Id I Will give complete program uploading and downloading of all types of files
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankita Jha
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic