Use ClassLoader#getResource() instead if its URI represents a valid local disk file system path.

URL resource = classLoader.getResource("resource.ext");
File file = new File(resource.toURI());
FileInputStream input = new FileInputStream(file);
// ...

If it doesn't (e.g. JAR), then your best bet is to copy it into a temporary file.

Path temp = Files.createTempFile("resource-", ".ext");
Files.copy(classLoader.getResourceAsStream("resource.ext"), temp, StandardCopyOption.REPLACE_EXISTING);
FileInputStream input = new FileInputStream(temp.toFile());
// ...

That said, I really don't see any benefit of doing so, or it must be required by a poor helper class/method which requires FileInputStream instead of InputStream. If you can, just fix the API to ask for an InputStream instead. If it's a 3rd party one, by all means report it as a bug. I'd in this specific case also put question marks around the remainder of that API.

Answer from BalusC on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › java – convert file to inputstream
Java - Convert File to InputStream | Baeldung
January 5, 2024 - @Test public void givenUsingPlainJava_whenConvertingFileToInputStream_thenCorrect() throws IOException { File initialFile = new File("src/main/resources/sample.txt"); InputStream targetStream = new FileInputStream(initialFile); } Let’s look at another method, where we can use DataInputStream to read binary or primitive data from a file:
🌐
Baeldung
baeldung.com › home › java › java io › java – write an inputstream to a file
Java - Write an InputStream to a File | Baeldung
August 20, 2025 - In that case, we need to make sure we keep reading until we reach the end of the stream: @Test public void whenConvertingInProgressToFile_thenCorrect() throws IOException { InputStream initialStream = new FileInputStream( new File("src/main/resources/sample.txt")); File targetFile = new File("src/main/resources/targetFile.tmp"); OutputStream outStream = new FileOutputStream(targetFile); byte[] buffer = new byte[8 * 1024]; int bytesRead; while ((bytesRead = initialStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } IOUtils.closeQuietly(initialStream); IOUtils.closeQuietly(outStream); }
🌐
W3Schools
w3schools.com › java › java_fileinputstream.asp
Java FileInputStream
This example uses FileInputStream to read a text file, one byte at a time, and print the result as characters:
🌐
Programiz
programiz.com › java-programming › fileinputstream
Java FileInputStream (With Examples)
Here, we have created an input stream that will be linked to the file specified by fileObject. The FileInputStream class provides implementations for different methods present in the InputStream class.
🌐
Coderanch
coderanch.com › t › 381870 › java › conversion-inputstream-fileinputstream
conversion from inputstream to fileinputstream (Java in General forum at Coderanch)
January 16, 2007 - As i said earlier the getResourceAsStream function returns InputStream as the url might point to different location, could be an entry in the jar file (thats what happening in your case) - in this case there is no file, So it returns a ByteArrayInputStream ... With other words, as the stream is not (directly) reading from a file, it cannot possibly be a FileInputStream...
🌐
Jenkov
jenkov.com › tutorials › java-io › fileinputstream.html
Java FileInputStream
August 28, 2019 - Note also, that since FileInputStream is a subclass of InputStream, we can cast the created FileInputStream to an InputStream everywhere we want to, as we do in the example above.
Find elsewhere
Top answer
1 of 3
1

From JavaDoc

A FileInputStream obtains input bytes from a file in a file system.

I would suggest two solutions:

  1. The proper one is to change the API and to have InputStream as a parameter. I don't see a reason why you have FileInputStream in your API.
  2. If you don't own the API and cannot change it I'm afraid you will need to save the InputStream to temp file and then create FileInputStream giving a path to this file (it's a suboptimal solution as you first write the file to disk - risking out of space - and then read it and streaming API is designed for reading / writing data on the fly)
2 of 3
0

If you are using org.apache.commons.fileupload.FileItem interface then your class is probably DefaultFileItem which is a subclass of DiskFileItem. So you can cast FileItem to DiskFileItem. then if you look at the source code of DiskFileItem you'll find that getInputStream() is actually returning a FileInputStream or a ByteArrayInputStream If you get a FileInputStream from DiskFileItem you can pass it directly to your other class. But if you get a ByteArrayInputStream you will have to write the contents to your own temporary file and then open another FileInputStream on this temp file. There is also another method DiskFileItem.getStoreLocation() which seem to return the server side File used for upload, but it may return null if the file is cached in memory.

In conclusion: you cannot be sure that there is going to be a server side file because the upload may be cached in memory. Therefore if you need a FileInputStream elsewhere you will have to create it yourself by creating a temp file. There is an example on how to pipe between two streams here.

🌐
Mkyong
mkyong.com › home › java › how to convert inputstream to file in java
How to convert InputStream to File in Java - Mkyong.com
December 27, 2020 - File file = new File("d:\\download\\google.txt"); InputStream inputStream = new FileInputStream(file); ... Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › FileInputStream.html
FileInputStream (Java Platform SE 7 )
SecurityException - if a security manager exists and its checkRead method denies read access to the file. ... Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
🌐
Attacomsian
attacomsian.com › blog › java-convert-inputstream-to-file
How to convert an InputStream to a File in Java
December 10, 2019 - To replace the existing file, you can use the below example code: try (InputStream stream = Files.newInputStream(Paths.get("input.txt"))) { // convert stream to file Files.copy(stream, Paths.get("output.txt"), StandardCopyOption.REPLACE_EXISTING); } ...
🌐
Javatpoint
javatpoint.com › java-fileinputstream-class
Java FileInputStream Class
Java FileInputStream Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, reader and writer class. The java.io package provides api to reading and writing data.
🌐
Initial Commit
initialcommit.com › blog › java-convert-inputstream-to-file
Java – Convert InputStream to File - Initial Commit
July 11, 2018 - public static void convertInputStreamToFileCommonsIO(InputStream is) throws IOException { OutputStream outputStream = null; try { File file = new File("C:\\Users\\user\\Desktop\\test\\output.txt"); outputStream = new FileOutputStream(file); IOUtils.copy(is, outputStream); } finally { if(outputStream != null) { outputStream.close(); } } } This tutorial shows several ways to convert InputStream to File in Java.
🌐
TraceDynamics
tracedynamics.com › java-file-to-inputstream
Java File To InputStream: Bridging File Data, Stream Processing
December 19, 2023 - Output: InputStream is: java.io.FileInputStream@15db9742 · As per output, the program reads a text file and converts a File to an InputStream.
🌐
Mkyong
mkyong.com › home › java › how to read file in java – fileinputstream
How to read file in Java - FileInputStream - Mkyong.com
January 1, 2021 - This example uses FileInputStream to read bytes from a file and print out the content. The fis.read() reads a byte at a time, and it will return a -1 if it reached the end of the file. ... package com.mkyong.io.api.inputstream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamExample1 { public static void main(String[] args) { readFile("c:\\test\\file.txt"); } private static void readFile(String fileName) { try (FileInputStream fis = new FileInputStream(new File(fileName))) { int content; // reads a byte at a time, if it reached end of the file, returns -1 while ((content = fis.read()) != -1) { System.out.println((char)content); } } catch (IOException e) { e.printStackTrace(); } } }
🌐
Programiz
programiz.com › java-programming › examples › load-file-as-inputstream
Java Program to Load File as InputStream
Here, we used the FileInputStream class to load the input.txt file as input stream. We then used the read() method to read all the data from the file. ... We can also load this Java file as input stream. import java.io.InputStream; import java.io.FileInputStream; public class Main { public ...