Statement of needs
In the backend, the Excel file stream is generated through POI. After converting the output stream (outputStream) to the input stream (inputStream), it is necessary to convert the input stream (inputStream) into a File object.
Question: If you need to convert the input stream (inputStream) into a File object, you must generate a File object according to the local path, that is to say, you must generate a file locally anyway
problem solved
After a series of data inquiries, it is found that the following methods can roughly meet the needs
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamUtil {
static final String PREFIX = "stream2file";//前缀字符串定义文件名;必须至少三个字符
static final String SUFFIX = ".tmp";//后缀字符串定义文件的扩展名;如果为null,则将使用后缀".tmp"
public static File stream2file (InputStream in) throws IOException {
final File tempFile = File.createTempFile(PREFIX, SUFFIX);
tempFile.deleteOnExit();
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(in, out);
}
return tempFile;
}
}
tempFile
After we run the above program, it will be the File object we need.
Seeing this, you may be curious, isn't this also generating a file locally? But the location of the file it generates is stored in the following (temporary file directory of the computer), so it can be seen that the file is not generated locally:
C:\Users\TP\AppData\Local\Temp\tmp2447618135336474361.txt