Java 如何在 Spring 中将 Multipart 文件作为字符串读取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31393553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:01:51  来源:igfitidea点击:

How to read a Multipart file as a string in Spring?

javaspringmultipartform-data

提问by nivedita rahurkar

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

我想使用 Advanced Rest Client 从我的桌面发布一个文本文件。这是我的控制器:

@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?

这不会返回任何字节长度或文件大小的值。我想将文件值读取到 StringBuffer。有人可以提供有关此的指示吗?我不确定在将其解析为字符串之前是否需要保存此文件。如果是这样,我如何将文件保存在工作区中?

回答by Rodrigo Villalba Zayas

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

首先,这与 Spring 无关,其次,您不需要保存文件来解析它。

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

要将 Multipart 文件的内容读入字符串,您可以像这样使用Apache CommonsIOUtils 类

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

回答by OlivierTerrien

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

如果要将 Multipart 文件的内容加载到 String 中,最简单的解决方案是:

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.

但是,如果您的文件很大,这个解决方案可能不是最好的。

回答by UsamaAmjad

The given answers are correct but the top answer said it's not efficient for large filesand 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 lineand Apache Commons IO provides a nice API for it.

给出的答案是正确的,但最重要的答案说它对大文件效率高,原因是它将整个文件保存在内存中,这意味着如果您上传 2gb 文件,它将消耗那么多内存。我们可以逐行读取文件,而不是这样做,Apache Commons IO 为其提供了一个很好的 API。

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

source

来源