如何在 Java 中读取多部分文件输入流的内容
How to read contents of a multipart file inputstream in Java
我有一个 Thymeleaf html 表单,它接受上传的文件作为输入,然后向 Java 控制器发出 post 请求以获取多部分文件。然后我将文件转换为输入流。虽然我能够读取文件的大小和输入类型,但我无法成功打印出内容。
例如,对于 .doc 文件,如果我尝试使用我发现的方法来打印文件内容,它只会打印出一系列数字。我假设是一种编码。是否存在打印上传的 .doc 文件内容的方法?
我目前用来尝试打印文件内容的控制器操作是:
@PostMapping("/file-upload")
public String uploadFile(@RequestParam("fileUpload") MultipartFile fileUpload, Model model) throws IOException {
InputStream fis = fileUpload.getInputStream();
for (int i = 0; i < fis.available(); i++) {
System.out.println("" + fis.read());
}
return "home";
}
我用来提交文件的表格是:
<form th:action="@{/file-upload}" enctype="multipart/form-data" method="POST">
<div class="container">
<div class="row" style="margin: 1em;">
<div class="col-sm-2">
<label for="fileUpload">Upload a New File:</label>
</div>
<div class="col-sm-6">
<input type="file" class="form-control-file" id="fileUpload" name="fileUpload">
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-dark">Upload</button>
</div>
</div>
</div>
</form>
不要使用 InputStream.available()。 来自 the documentation:
It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
只有从 read() 中得到 -1 的值表示 InputStream 结束。
For example, for a .doc file, if I try methods I have found to print out the file's contents, it merely prints a series of numbers. Which I'm assuming is an encoding.
您的假设不正确。 .doc 文件是复杂的二进制格式,而不仅仅是文本编码。 (尝试在记事本中打开 .doc 文件。)
您正在获取数字,因为您正在打印数字。 InputStream.read() returns 一个整数。 "" + fis.read()
将每个返回的 int 转换为 String。
如果真的要打印文件内容,直接写字节:
int b;
while ((b = fis.read()) >= 0) {
System.out.write(b);
}
如果您使用的是 Java 9 或更高版本,您可以只使用:
fis.transferTo(System.out);
但是,这两个选项都不会以可读形式显示 Word 文档的内容。您将需要一个可以从 Word 文件中读取文本内容的库,例如 Apache POI。 (还有其他可用的库;您可能需要搜索它们。)
我有一个 Thymeleaf html 表单,它接受上传的文件作为输入,然后向 Java 控制器发出 post 请求以获取多部分文件。然后我将文件转换为输入流。虽然我能够读取文件的大小和输入类型,但我无法成功打印出内容。
例如,对于 .doc 文件,如果我尝试使用我发现的方法来打印文件内容,它只会打印出一系列数字。我假设是一种编码。是否存在打印上传的 .doc 文件内容的方法?
我目前用来尝试打印文件内容的控制器操作是:
@PostMapping("/file-upload")
public String uploadFile(@RequestParam("fileUpload") MultipartFile fileUpload, Model model) throws IOException {
InputStream fis = fileUpload.getInputStream();
for (int i = 0; i < fis.available(); i++) {
System.out.println("" + fis.read());
}
return "home";
}
我用来提交文件的表格是:
<form th:action="@{/file-upload}" enctype="multipart/form-data" method="POST">
<div class="container">
<div class="row" style="margin: 1em;">
<div class="col-sm-2">
<label for="fileUpload">Upload a New File:</label>
</div>
<div class="col-sm-6">
<input type="file" class="form-control-file" id="fileUpload" name="fileUpload">
</div>
<div class="col-sm-4">
<button type="submit" class="btn btn-dark">Upload</button>
</div>
</div>
</div>
</form>
不要使用 InputStream.available()。 来自 the documentation:
It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
只有从 read() 中得到 -1 的值表示 InputStream 结束。
For example, for a .doc file, if I try methods I have found to print out the file's contents, it merely prints a series of numbers. Which I'm assuming is an encoding.
您的假设不正确。 .doc 文件是复杂的二进制格式,而不仅仅是文本编码。 (尝试在记事本中打开 .doc 文件。)
您正在获取数字,因为您正在打印数字。 InputStream.read() returns 一个整数。 "" + fis.read()
将每个返回的 int 转换为 String。
如果真的要打印文件内容,直接写字节:
int b;
while ((b = fis.read()) >= 0) {
System.out.write(b);
}
如果您使用的是 Java 9 或更高版本,您可以只使用:
fis.transferTo(System.out);
但是,这两个选项都不会以可读形式显示 Word 文档的内容。您将需要一个可以从 Word 文件中读取文本内容的库,例如 Apache POI。 (还有其他可用的库;您可能需要搜索它们。)