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 Creating file and sending on servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating file and sending on servlet" Watch "Creating file and sending on servlet" New topic
Author

Creating file and sending on servlet

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
I have a class that creates a file and send it over servlet. But the issue with the approach is that the server box where this application runs, the file gets created in the current directory of that app. I do not want the file to be saved on the application box ,as the end client can request multiple of these files over the period, in a day and the box should not run out of space.

Below is the code that does that.



The above code creates the file xyz.xml and send it to client to download. But when the client requests for the file, it also creates the file in the application directory. Which I do not want. How to avoid that?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

trupti nigam wrote:The above code creates the file xyz.xml

I see no such code.

It's also a really bad practice to pass a response around, but before we address that we need to know what you are really doing. The code you posted and the description of what it does do not match.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Bear Bibeault wrote:
trupti nigam wrote:The above code creates the file xyz.xml

I see no such code.

It's also a really bad practice to pass a response around, but before we address that we need to know what you are really doing. The code you posted and the description of what it does do not match.


The code creates the file with the "fileName" that is passed to the method.

This is what I do.
1. Client can request a file for different products by passing the product name via the URL
2. Server will get the product Name, build the xml response and create file.
3. Send it over the servletOutPutStream. The End client will be able to get the file in fileName.xml format. The code is working fine. But it also drops the same file on the server box where the app runs. I want to avoid that.

thanks
Trupti
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

It's possible that the code underlying "secResponse.toString()" writes a file to the current working directory on the server as a side-effect. Or it's possible that some other part of creating that SecurityListImpl object does that. But in any case it should be obvious that nobody here can see the code which does that. It's going to be up to you to look through the source code for SecurityListImpl -- or whatever class is actually passed to that method -- to find that code.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

If you're just going to stream the XML markup, why create a file in the first place?

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Paul Clapham wrote:It's possible that the code underlying "secResponse.toString()" writes a file to the current working directory on the server as a side-effect. Or it's possible that some other part of creating that SecurityListImpl object does that. But in any case it should be obvious that nobody here can see the code which does that. It's going to be up to you to look through the source code for SecurityListImpl -- or whatever class is actually passed to that method -- to find that code.



When I debugged the code the secResponse.toString() line dumps the file on the box. The creation of SecurityListImpl object has nothing to do with this file. When I create this object, I just assign the value to it.
How can I avoid creation of file during secResponse.toString()?

thanks,
Trupti
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

How can we say when you're keeping the important parts of the code a secret?

Generally, rather than stream the XML to a file, stream it to the response output stream. There's rarely a need to create an intermediate disk file just to stream something as a response.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Bear Bibeault wrote:How can we say when you're keeping the important parts of the code a secret?

Generally, rather than stream the XML to a file, stream it to the response output stream. There's rarely a need to create an intermediate disk file just to stream something as a response.


What part of code you would like to see. I shared the code that is being used to actually create the file and send it over HTTPResponse.
Also I am streming the xml to the response output stream.



The requirement is such that, when the end client requests a product details the server should be able to send it to end client in the form of xml file.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
I found the issue.
I had below line in the code that I forgot to comment out which was dumping the file to the box.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Well, obviously that was the line of code we would have wanted to see.

But anyway, you've found and fixed the problem, and that's the main thing we want to happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating file and sending on servlet