I have a HttpServletResponse object and need to write a file contained in the jar. The following code segments do not work for me.
URI uri = <myclass>.class.getResource("/" + filename).toURI();
PrintWriter out = response.getWriter();
File f = new File(uri);
FileReader bis = new FileReader(f);
char[] buff = new char[1024];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
out.write(buff, 0, bytesRead);
}
I know that this will work
InputStream inputStream = <myclass>.class.getResourceAsStream("/" + filename);
but I cannot get the PrintWriter out.write to write the inputStream.
Can anyone tell me how this can be done.
Thanks
Since Java 7, you can do it in one line even without using any external libraries: Files. copy(inputStream, outputPath, StandardCopyOption. REPLACE_EXISTING);
Using nio packages exposed by Java 8, you can write an InputStream to a File using Files. copy() utility method.
Resolved using the following
InputStream inputStream = KCSSchemaController.class.getResourceAsStream("/" + schemaname);
OutputStream output = response.getOutputStream();
ByteStreams.copy(inputStream, output);
output.flush();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With