How to convert InputStream to virtual File

前端 未结 2 1104
时光说笑
时光说笑 2020-11-29 02:27

I have a method which expects the one of the input variable to be of java.io.File type but what I get is only InputStream. Also, I cannot change the signature of the method.

2条回答
  •  一向
    一向 (楼主)
    2020-11-29 02:55

    You can't. The input stream is just a generic stream of data and there is no guarantee that it actually originates from a File. If someone created an InputStream from reading a web service or just converted a String into an InputStream, there would be no way to link this to a file. So the only thing you can do is actually write data from the stream to a temporary file (e.g. using the File.createTempFile method) and feed this file into your method.

提交回复
热议问题