1

I am using Spring Frame work 3.05 and i wanted download files using same framework.After googling i found answer in this stack overflow link .

Downloading a file from spring controllers

in the above link -> Scott Carlson - Answer

But if i try to do as suggested in the above link. I am getting following Error :

" Error while writing to output stream due to :java.io.NotSerializableException: org.springframework.core.io.FileSystemResource "

My code looks Like :

 FileSystemResource fsr = new FileSystemResource(path); try { OutputStream outstr = res.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstr); oos.writeObject(fsr); oos.flush(); oos.close(); } catch(Exception e) { log4log.error("Error while writing to output stream due to :"+e.toString()); return null; } 

Please Help me out...

2
  • you can not serialise the Stream itselb. You have to read the data from strem and then you can serialize it. Commented Jan 25, 2016 at 7:50
  • Scott Carlson's answer doesn't do, at all, what you're doing in your code. Just read it again. Commented Jan 25, 2016 at 7:55

1 Answer 1

1

What you want to do is to copy the contents your FileSystemResource into the response's Outputstream:

InputStream in = fsr.getInputStream(); OutputStream out = response.getOutputStream(); org.apache.commons.io.IOUtils.copy(in, out); 

What you were trying to do in your code is actually trying to serialize an object into the output stream of the response.

Sign up to request clarification or add additional context in comments.

1 Comment

The image file downloading in browser.But when i try download in nodejs server ,its getting corrupted.Actuly file size of downloaded file is 1.8Mb insted of 1 Mb. How to solve this...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.