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...

Community
  • 1
  • 1
  • you can not serialise the Stream itselb. You have to read the data from strem and then you can serialize it. – Jens Jan 25 '16 at 07:50
  • Scott Carlson's answer doesn't do, at all, what you're doing in your code. Just read it again. – JB Nizet Jan 25 '16 at 07:55

1 Answers1

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.

Community
  • 1
  • 1
nobeh
  • 9,784
  • 10
  • 49
  • 66
  • 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... – shadanana upadhya Jan 25 '16 at 08:47