简体   繁体   English

如何在 Java 中读取多部分文件输入流的内容

[英]How to read contents of a multipart file inputstream in Java

I have a Thymeleaf html form that accepts an uploaded file as input and then makes a post request to a Java controller for the multipart file. I have a Thymeleaf html form that accepts an uploaded file as input and then makes a post request to a Java controller for the multipart file. I then convert the file into an inputstream.然后我将文件转换为输入流。 While I am able to read the file's size and input type, I am not able to successfully print out the contents.虽然我能够读取文件的大小和输入类型,但我无法成功打印出内容。

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.例如,对于一个 .doc 文件,如果我尝试使用我找到的打印文件内容的方法,它只会打印一系列数字。 Which I'm assuming is an encoding.我假设是一种编码。 Does a method exist to print out the contents of an uploaded.doc file?是否存在打印出上传的.doc 文件内容的方法?

The controller action I'm currently using to attempt to print out the file's contents is:我目前用来尝试打印文件内容的 controller 操作是:

@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";
}

And the form I am using to submit the file is:我用来提交文件的表格是:

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

Do not use InputStream.available().不要使用 InputStream.available()。 From 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.使用此方法的返回值来分配用于保存此 stream 中所有数据的缓冲区永远是不正确的。

Only getting value of -1 from read() indicates the end of the InputStream.只有从 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.例如,对于一个 .doc 文件,如果我尝试使用我找到的打印文件内容的方法,它只会打印一系列数字。 Which I'm assuming is an encoding.我假设是一种编码。

Your assumption is incorrect.你的假设是不正确的。 A.doc file is complex binary format, not just a text encoding. A.doc 文件是复杂的二进制格式,而不仅仅是文本编码。 (Try opening a.doc file in Notepad.) (尝试在记事本中打开 a.doc 文件。)

You are getting numbers because you are printing numbers.你得到数字是因为你在打印数字。 InputStream.read() returns an int. InputStream.read() 返回一个 int。 "" + fis.read() converts each returned int to a String. "" + fis.read()将每个返回的 int 转换为字符串。

If you really want to print the contents of the file, write the bytes directly:如果你真的想打印文件的内容,直接写字节:

int b;
while ((b = fis.read()) >= 0) {
    System.out.write(b);
}

If you're using Java 9 or later, you can just use:如果您使用的是 Java 9 或更高版本,则可以使用:

fis.transferTo(System.out);

However, neither option will show the contents of a Word document in a readable form.但是,这两个选项都不会以可读形式显示 Word 文档的内容。 You will need a library that can read the text content from a Word file, like Apache POI .您将需要一个可以从 Word 文件中读取文本内容的库,例如Apache POI (There are other libraries available; you may want to search for them.) (还有其他可用的库;您可能需要搜索它们。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM