Get the content of MultipartFile

12,044

Only difference is that with getBytes() the data has already been read from the stream, whereas with getInputStream() you will still have to read the data.

What you use depends on what you want to do with the content. If its an image that you just want to write out to disk, then getBytes() would be best, but if it is Text that you want to parse and do something with, then getInputStream() might be better.

Share:
12,044
Lydon Ch
Author by

Lydon Ch

Updated on June 04, 2022

Comments

  • Lydon Ch
    Lydon Ch almost 2 years

    I am trying to get the content of MultipartFile, which is obtained through MultipartHttpServletRequest.getFile().

    There are 2 functions in MultipartFile:

    • bytes[] getBytes() ()

    • InputStream getInputStream()

    What is the most efficient way to get the content? (Which method would you use?)

  • Lydon Ch
    Lydon Ch almost 14 years
    the content of the file is string (xml file). So I ended up doing new String( part.getBytes()).
  • stivlo
    stivlo over 12 years
    parsing XML is in fact an example where getInputStream() can be used, as SAXParser parse() method accepts InputStream. Said that, if your XML is in the kb range and not in the Mb range, it doesn't matter.