1

I'm working on a spring mvc application. at this time i need to have action for file downloading. due to this post my controller's action now is like this:

 @RequestMapping(value = "/file/{id}", method = RequestMethod.GET, produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void getFile(@PathVariable("id") int id, HttpServletResponse response) throws FileNotFoundException {

    //fetch file record from database
    Files file = orchService.fileFind(id);

    //directory of the file is based on file ID(this is not important in this question)
    InputStream inputStream = new FileInputStream(myFileDirectory);
    response.setHeader("Content-Disposition", "attachment; filename=\"filename " + file.getId() + "."+file.getExtension()+"\"");
    int read=0;

    byte[] bytes = new byte[];

    //remaining of code
}

my problem is on the bytes declaration. file has a long getSize() method that return the size of the files. but i can't use byte[] bytes = new byte[file.getSize()];, because the array size must be an integer value. how can i solve this problem?

I don't want to copy whole file into memory.

Robert
  • 39,162
  • 17
  • 99
  • 152
hamed
  • 7,939
  • 15
  • 60
  • 114
  • If you really want to copy the whole file into memory (which I doubt), you can use `byte[] bytes = Files.readAllBytes(file);` – gknicker Jan 10 '15 at 07:40
  • i don't want to copy whole file into memory. because of OutOfMemory exception in large files – hamed Jan 10 '15 at 07:42

2 Answers2

2

Use IOUtils and just copy streams (file stream to response stream)

copy(InputStream input, OutputStream output)

Copies bytes from an InputStream to an OutputStream.

copy(InputStream input, OutputStream output, int bufferSize)

Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I see documentation and IOUtils has static "copy" method with 3 parameter, but netbeans gives me error: "no suitable method...". netbeans doesn't suggest copy method with 3 parameter!!! my netbeans version is 8.0 – hamed Jan 10 '15 at 10:49
  • Check the apache commons version you use in the project. – StanislavL Jan 10 '15 at 10:54
  • I have "commons-io-2.4.jar" in my project dependencies. – hamed Jan 10 '15 at 10:59
  • Check whether the version has the method. If not upgrade to the version where the method is supported – StanislavL Jan 10 '15 at 11:02
1

Read your data in a loop with a buffer of bytes. Reading to large byte arrays could be a performance issue.

If you need to save the data use a file or stream.

Reading a binary input stream into a single byte array in Java

Community
  • 1
  • 1
Daniel Persson
  • 602
  • 7
  • 17