32

I want to post a text file from my desktop using Advanced Rest Client. This is my controller:

@RequestMapping(value = "/vsp/debug/compareConfig/{deviceIp:.*}", method = RequestMethod.POST, consumes = { "multipart/form-data" }, produces = { "application/json" })

public ResponseEntity<SuccessResult> compareCLIs(HttpServletRequest request, @RequestParam("file") MultipartFile file, @PathVariable("deviceIp") String device) 
{
log.info(file.getOriginalFilename());
byte[] bytearr = file.getBytes();
log.info("byte length: ", bytearr.length);
log.info("Size : ", file.getSize());

}

This does not return any value for byte length or file size. I want to read the file values to a StringBuffer. Can someone provide pointers regarding this? I am not sure if I need to save this file before parsing it to a string. If so how do I save the file in the workspace?

2
  • 3
    You should avoid retrieving all the bytes at once. Instead, use MultiPartFile#getInputStream and use that stream to fill your StringBuilder (you don't need to use StringBuffer) or any other way to consume the data. Commented Jul 13, 2015 at 21:17
  • 1
    hi..did you get the solution. please add the solution. Commented Nov 19, 2020 at 9:07

3 Answers 3

53

If you want to load the content of a Multipart file into a String, the easiest solution is:

String content = new String(file.getBytes());

Or, if you want to specify the charset:

String content = new String(file.getBytes(), StandardCharsets.UTF_8);

However, if your file is huge, this solution is maybe not the best.

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

2 Comments

This is not working for me, doesn't return UTF characters.
Once the data is sent from the client, to my controller method. Is the entire file not loaded in memory at this point? Would reading the file at once cause problem then? @OlivierTerrien
13

First, this is not related to Spring, and, second, you don't need to save the file to parse it.

To read the content of a Multipart file into a String you can use Apache Commons IOUtils class like this

ByteArrayInputStream stream = new   ByteArrayInputStream(file.getBytes());
String myString = IOUtils.toString(stream, "UTF-8");

1 Comment

MultipartFile is a Spring type -- so the question is related to Spring. But once you call getBytes() you've entered the domain of generic Java.
3

The given answers are correct but the top answer said it's not efficient for large files and the reason is that it keeps the whole file in memory which means if you are uploading a 2gb file it will consume that much of memory. Instead of doing that we can read the file line by line and Apache Commons IO provides a nice API for it.

LineIterator it = FileUtils.lineIterator(theFile, "UTF-8");
try {
    while (it.hasNext()) {
        String line = it.nextLine();
        // do something with line
    }
} finally {
    LineIterator.closeQuietly(it);
}

source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.