简体   繁体   English

将输入流写入文件后,Java不会释放该过程

[英]Java does not release the process after writing inputstream to file

I am building a java RestService and try to upload an Image from the frontend and try to save it somewhere on my computer. 我正在构建Java RestService,并尝试从前端上传图像,然后尝试将其保存在计算机上的某个位置。 The Problem is that I only can open the uploaded image after I closed the server. 问题是我仅在关闭服务器后才能打开上载的图像。 When I try to open the image while the server is still running, I just get a black image... 当服务器仍在运行时尝试打开图像时,我只会得到黑色图像...

This is my code: 这是我的代码:

private void writeToFile(InputStream uploadedInputStream,
                         String uploadedFileLocation) {

    try {
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

That is the code where I call writeToFile()... 那是我调用writeToFile()的代码...

 @POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {


    String uploadedFileLocation = "c://Users/leona/Desktop/EOT/src/main/Backend_EOT/sparti-meetme-63dfc731c375/meetmeserver/src/main/webapp/" + name;

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    try {
        uploadedInputStream.close();
    }catch (Exception e) {
        log.debug("Input Stream konnte nicht geschlossen werden");
    }

    return Response.status(200).entity(output).build();

}

I really would appreciate if someone could help me with that. 如果有人可以帮助我,我将非常感激。

This is the right way to write data to a file, from an arbitrary input stream. 这是从任意输入流向文件写入数据的正确方法。 This ensures that the file descriptor will be flushed and closed. 这确保了文件描述符将被刷新并关闭。

private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
    try (OutputStream out = Files.newOutputStream
              (Paths.get(uploadedFileLocation), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
        int read = 0;
        byte[] bytes = new byte[8192];
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

You're opening 2 FileOutputStream s but only closing one of them. 您正在打开2 FileOutputStream但仅关闭其中之一。 here is a snippet of your code with a comment added by me, showing which line to delete: 这是您的代码的片段,其中有我添加的注释,显示要删除的行:

try {
    OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
    int read = 0;
    byte[] bytes = new byte[1024];

    // delete the following line, you already opened the output stream right after the `try`.
    out = new FileOutputStream(new File(uploadedFileLocation));

As an aside, make sure you close the uploadedInputStream . 另外,请确保关闭上uploadedInputStream It's not being closed in the code you posted. 您发布的代码中未将其关闭。 The best practice would be to close it in the same scope you create it in, which in this case would be outside of the function you posted. 最佳做法是在创建它的同一范围内关闭它,在这种情况下,该范围将超出您发布的功能。

Update: 更新:

Now that we see more of the code, we see that you're being passed an InputStream by a framework. 现在,我们看到了更多的代码,我们看到框架正在将您传递给InputStream Let the framework handle closing it. 让框架处理关闭它。 As mentioned above, the best practice is to close streams in the scope they are created in. In this case, that would be the Jersey framework. 如上所述,最佳实践是在创建流的范围内关闭流。在这种情况下,这就是Jersey框架。

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

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