Display a pdf file in browser? [duplicate] - java

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how to display a pdf file in jsp using servlet
I retrieve a pdf file from my database and put it in a file like this
String str="select * from files where name='Security.pdf';";
Statement stmt2= conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt2.executeQuery(str);
while(rs.next())
{
InputStream input = rs.getBinaryStream("content");
//to create file
File f=new File("c:/pdfile.pdf");
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=input.read(buf))>0)
out.write(buf,0,len);
out.close();
input.close();
System.out.println("\nFile is created..");
}
Now this is at server end. In my client side, Whenever user clicks a link say
a href=pdf(pdf is my servlet name) in my jsp page, I should display the file retrieved from database on client's Browser.
What should I do?

Set your content-type of the response to pdf
response.setContentType("application/pdf");
Then write the pdf contents into the response object

Don't save the PDF to a file on the server, just send it back to the browser as the servlet's response. Basically, instead of that FileOutputStream, use the OutputStream that you get from calling getOutputStream() on your ServletResponse object. You'll also need to set a Content-Type header so that the browser knows it's a PDF file.
Having a servlet write to a hard-coded path like that is dangerous because several instances of the servlet can run at the same time, in different threads. (Think about what happens if two people enter your servlet's URL in their browsers at the same time.) If they're both writing to the same file at the same time, they'll end up corrupting it.

Related

Why am I downloading a file with no extension using servlet?

I'm downloading a file without an extension when I go to a servlet
This is code of doGet method (these are just test lines, don't take them seriously):
try {
PrintWriter pw = response.getWriter();
pw.write("test");
pw.println(request.getParameter("a"));
DAOFactory m = DAOFactory.getDAOFactory(1);
Connection conForTests = MySQLDAOFactory.getConnection();
UserDao s = m.getUserDao();
boolean check = s.validateUser("test1","test1",conForTests);
pw.write(String.valueOf(check));
User user = s.findUser("test1",conForTests);
int id = user.getUserId();
pw.write(11);
} catch (SQLException|IOException sqlException) {
System.out.println("asdsad");
sqlException.printStackTrace();
}
System.out.println("asdsad");
}
And I checked all the lines removing them line by line and I have found out that at this line:
pw.write(11);
And that's 11 was a user id so to not retrieve that id each time, I have just written 11. The servlet starts not showing a page, but downloading a file without an extension.
I checked that 11 number is staying for a Vertical Tab in ASCII table. Why is 11 code in ASCII table makes browser to not displaying but downloading file?
And that is content of this file:
Why am I downloading a file with no extension using servlet?
Because you just opened a response stream and started writing into it. In lay terms, you are just sending some bytes back to the browser, but the browser doesn't know what does bytes are. Is it html? Is it plain text? Is it an image? Some other thing?
So before starting to write the response, you need to say what that response is by setting a content type. Replace this code of yours:
try {
PrintWriter pw = response.getWriter();
....
with:
try {
response.setContentType("text/html")
PrintWriter pw = response.getWriter();
....
or whatever content type you prefer (a text/plain can also work for what you are doing).
See also:
what is the use of "response.setContentType("text/html")" in servlet
Java - Default contentType for Servlet

Download a file from a browser in a Spring MVC application [duplicate]

This question already has answers here:
Using jQuery AJAX to download a binary file
(4 answers)
Closed 5 years ago.
I have a requirement to download a csv file from a Spring MVC application. I have the data which I need to include in the file. I need to create a file on the fly in the browser session and download it to the user system. I tried doing the solution as mentioned here: Downloading a file from spring controllers
I was unable to produce the csv and it is looking for a file on the server.
What I have done so far:
Client Side Call:
$.ajax({
type: "GET",
url:"${ctx}/emp/getAttendance?empId=" + emp +"&fileName=" + file,
success: function (data) {
$('#result_loading').hide();
}
});
Server Side:
String path = req.getServletContext().getRealPath("");
File file = new File(path + "/" + fileName);
file.createNewFile();
FileInputStream inputStream = new FileInputStream(file);
resp.setContentType("application/pdf");
resp.setHeader("content-disposition;","attachment;filename=\"download.pdf\"");
OutputStream outputStream = resp.getOutputStream();
IOUtils.copy(inputStream, outputStream);
outputStream.close();
I need the Ajax call to download the file. I don't see even an empty file downloaded.
Can someone help me with this on where I am doing wrong.
You can use the FileSystemResource class from the spring framework
the class exists in the location
org.springframework.core.io.FileSystemResource
Write your data onto a file (on the physical server) by creating a csv file using the java.io.File Class and then pass the value of the file object to the constructor
FileSystemResource(File file)
and return this to the browser, the browser will automatically open up a download window once the url is loaded.
Delete the file once the file system resource is initialized, and you would be as good as you never created the file.

open pdf file with sessionAsSigner

I have a database where the user doesn't has access to.
Still I can go to the database and "read" the documents with for example
var db:NotesDatabase = sessionAsSigner.getDatabase("","somedir/some.nsf");
In this database there's a pdf file I would like to open or download. I have the filename and the unid . If the user had acces to the database I could do it with
http(s)://[yourserver]/[application.nsf] /xsp/.ibmmodres/domino/OpenAttachment/ [application.nsf]/[UNID|/$File/[AttachmentName]?Open
How can I do it with sessionAsSigner without putting a $PublicAccess=1 field on the form ?
edit:
the pdf file is stored as attachment in a richtextfield
second edit
I'm trying to use the XSnippet from Naveen and made some changes
The error message I get is : 'OutStream' not found
The code I tried is :
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + zipFileName);
var embeddedObj:NotesEmbeddedObject = null;
var bufferInStream:java.io.BufferedInputStream = null;
var outStream:java.io.OutputStream = response.getOutputStream();
embeddedObj = downloadDocument.getAttachment(fileName);
if (embeddedObj != null) {
bufferInStream = new java.io.BufferedInputStream(embeddedObj.getInputStream());
var bufferLength = bufferInStream.available();
var data = new byte[bufferLength];
bufferInStream.read(data, 0, bufferLength); // Read the attachment data
ON THE NEXT LINE IS THE PROBLEM
OutStream.write(data); // Write attachment into pdf
bufferInStream.close();
embeddedObj.recycle();
}
downloadDocument.recycle();
outStream.flush();
outStream.close();
facesContext.responseComplete();
Create an XAgent (= XPage without rendering) which takes datebase + documentid + filename as URL parameters and delivers the file as response OutputStream.
The URL would be
http(s)://[yourserver]/download.nsf/download.xsp?db=[application.nsf]&unid=[UNID]&attname=[AttachmentName]
for an XAgent download.xsp in a database download.nsf.
The code behind the XAgent runs as sessionAsSigner and is able to read the file even the user itself has no right to access file's database.
Use Eric's blog (+ Java code) as a starting point. Replace "application/json" with "application/pdf" and stream pdf file instead of json data.
As an alternative you can adapt this XSnippet code from Thomas Adrian. Use download() together with grabFile() to write your pdf-File to OutputStream.
Instead of extracting attachment file to path and reading it from there you can stream the attachment right from document to response's OutputStream. Here is an XSnippet from Naveen Maurya as a good example.
If you can get the PDF file as a stream, you should be able to use the OutputStream of the external context's response.
Stephan Wissel has a blog posting about writing out an ODF file so you should be able to cut that up as a starting point.
http://www.wissel.net/blog/d6plinks/SHWL-8248MT
You already have the db so, you will just need to know the UNID of the document.
var doc = db.getDocumentByUNID(unid) 'unid is a supplied param
var itm:RichTextItem = doc.getFirstItem("Body") 'assuming file is in body field
Once you have the itm, you can loop round all of the embeddedObjects and get the pdf file. At this point, I don't know if you can stream it directly or if you have to detach it, but assuming you detach it, you will then use something like this.
File file = new File("path to file");
FileInputStream fileIn = new FileInputStream(file);
Don't forget to clean up the temporarily detached file

java: file read, modify outside, then read again, modification not seen

I have set of files which will be served by a servlet based on a parameter. The servlet opens stream to required file, reads, writes to http response and closes the stream. Now this file can be modified manually and saved. Then next time a request comes for this file, servlet opens a stream to this file, writes it to the response, but I see the content is not the modified one, its the old one.
If I restart tomcat, servlet responds with modified content. Can it be made to read the modified content without restart?
Its not tomcat caching, the string read from file reader itself is unchanged.
thanks!
Here's the content of the servlet-
InputStream fhandle=this.getClass().getClassLoader().getResourceAsStream("responses/"+file);
if(fhandle!=null){
Reader fr=new InputStreamReader(fhandle);
PrintWriter out=resp.getWriter();
int a=-1;
FileWriter fos=new FileWriter("copyFile.xml"); //to compare what's read with actual file
while((a=fr.read())>=0){
fos.write(a);
out.write(a);
}
out.flush();
fos.close();
fr.close();
fr=null;
fhandle.close();
fhandle=null;
System.gc();
}
else
System.out.println(file+" not found");
After hitting the servlet for first time, I will edit the file and save it. Now again I will hit the same servlet for the same file, and I will not get the edited content.
Resources are not reloaded dynamically when the associated files change. Resources are part of the source tree, distributed with the application. They aren't expected to change. There is something seriously wrong with your design if you require this behaviour.

open/save file in smartGWT

I have implemented RPCService, RPCServiceAsync & RPCServieImpl. On clicking a button a service in server side will be called and it will fetch data from DB and file is created. Once the file is created, then i need to open that file in client side and need to prompt a dialog box with open/save options.
how can i implement this opening a file part. pls suggest a way to implement t.. Reply pls.. thanks in advance....
#Hambend : I still have one more clarification !.. how to call this doGet method in another servlet i.e. in my onmodule load class i am having lot of widgets in seperate layout and one such a widget is BUTTON ? onclicking this button service RPCServiceImpl is called and all manipulations are done and file is created in a serperate function (public int GenerateFile(String name)() ) . how to make that function to call doGet method ? since doGet needs request,response parameters to be passed along with it?? pls suggest me a method to call that method. thanks in advance
GWT's RPC services are only able to send java objects back to the client. To send a file back to the user you'll need to use a non-GWT java servlet. Here's some code I've used before for serving up jpeg images from a repository.
public class ImageServlet extends HttpServlet {
private final String repository = "/var/images/";
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = request.getParameter("file");
// Security: '..' in the filename will let sneaky users access files
// not in your repository.
filename = filename.replace("..", "");
File file = new File(repository + filename);
if (!file.exists())
throw new FileNotFoundException(file.getAbsolutePath());
response.setHeader("Content-Type", "image/jpeg");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-disposition", "attachment;filename=\"" + filename + "\"");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
byte[] buf = new byte[1024];
while (true) {
int length = bis.read(buf);
if (length == -1)
break;
bos.write(buf, 0, length);
}
bos.flush();
bos.close();
bis.close();
}
}
"Content-disposition: attachment" should cause most browsers to download the file instead of displaying it, with the filename defaulting to whatever you provide. The way you would use this servlet is to have the user call the RPCService that you already have, which saves the file to the repository folder. Then, you link or redirect them to this servlet with a url such as http://your.domain.com/fileServlet?file=myFile.jpg. Obviously with this setup you have a security risk where users can download other people's files if they can guess the filenames.
What you might like to do is merge the database code from your RPC service into this servlet. There's no need to save the file anywhere on the server, you can take your database results and write them into response.getOutputStream() or response.getWriter() in exactly the same way you would write them to file, except that the result goes straight to the user. As long as you set your content headers correctly the user won't notice the difference.
You can't call this method from another servlet, the only way to make a browser to download it as a file is to access it through a normal HTTP request. First you declare the servlet in your web.xml file like you would a GWT RPC service:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>
com.package.ImageServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/imageServlet</url-pattern>
</servlet-mapping>
Now any HTTP GET requests going to http://your.tomcat.server/webapp/imageServlet will get picked up by ImageServlet.doGet(). Then on the client side you can either make a normal html link to the file:
new HTML("<a href='" + GWT.getHostPageBaseURL() + "imageServlet?file=" + filename + "'>download</a>");
...or, you should be able to put this in a ClickHandler (I haven't tested it):
Window.Location.assign(GWT.getHostPageBaseURL() + "imageServlet?file=" + filename);
If the file is text, then you always back back an Object with an array of Strings.
If binary then simply a byte[] in the class would do.

Categories

Resources