• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic java io question  RSS feed

trupti nigam
Ranch Hand
Posts: 640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have written a servlet that downloads the file to client machine.
In the servlet I am creating a file like below.
private ObjectOutputStream createFile(List list,Market[] markets) throws IOException
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(thisFile));
for(int i=0 ;i<list.size();i++){
Object market = list.get(i);
return oos;


List is the java List object and markets is the array of type Market.
I create the response as below;

public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";

response.setHeader("Content-Disposition","attachment;filename=\"" + "xmlfile2.xml" + "\"");
ServletOutputStream op = response.getOutputStream();

But when I download the file, the file does not contain the data. it Just has
java.io.ObjectOutputStream@114a707 line in the file.

I debugged my code and I can see that there are objects in List and the market array is not empty.
Why the file is not saving the data? What am I doing wrong?


Ulf Dittmer
Posts: 42972
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is object "o" - the ObjectOutputStream? If so, o.toString() doesn't do anything useful, especially after the stream has been closed.

Instead of a FileOutputStream, use a ByteArrayOutputStream so that you capture the bytes that make up the serialized object. Then you can stream those to the client.

But those are binary, not XML (which you seem to assume, judging by the file extension in the code). What is the client supposed to be able to do with the serialized object representation? If you want XML, and the objects are Java Beans, you can use java.beans.XMLEncoder to get an XML serialization.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!