我已经将图像从 html 存储到输入流,所以我想将输入流存储到我驱动器中的文件夹

I have stored image to inputstream from html,so i want to store the inputstream to folder in my drive

Part filePart = request.getPart("barcodePhtotoVen");

inputStream = filePart.getInputStream();

如何使用 outputstream 将此写入我的计算机驱动器中的 folder/file?

我认为这个问题已经被问了很多,这个答案提供了几种将输入流写入文件的选项:

Easy way to write contents of a Java InputStream to an OutputStream

希望以下代码片段对您有所帮助

更新:

 OutputStream out = null;
 InputStream filecontent = null;

 try {
    out = new FileOutputStream(new File("destination_file_path"));
    filecontent = filePart.getInputStream();

    int read = 0;
    final byte[] bytes = new byte[1024];

    while ((read = filecontent.read(bytes)) != -1) {
        out.write(bytes, 0, read);
    }
} catch (FileNotFoundException f) {

} finally {
  if (out != null) {
        out.close();
    }
}

我在我的系统上测试的早期代码,完美运行。请尝试我的更新代码。我刚在我的系统上测试过,它也工作正常。

资源Hope this knowledge sharing help you.

谢谢。

不要使用 InputStream,也不要使用 OutputStream。零件有一个 write(String) method 将零件直接写入文件。

I have stored image to inputstream

不,你没有。您没有将图像存储在任何地方,更不用说输入流了,这在术语上是自相矛盾的。您需要从 输入流中读取 并写入磁盘文件。