I created a file and writing an input stream to it but I am getting this exception with just this info IOException: Pipe closed.
Here is my code:
File outputFile = new File(filePath);
try (OutputStream outputStream = new FileOutputStream(outputFile)) {
IOUtils.copy(inputStream, outputStream); // exception here
} catch (IOException e) {
throw new FileWritingException("Exception while writing file::" + e.getMessage());
}
I also tried adding the following but still the same error. Only empty file gets created in that output location.
File file = new File(filePath);
try (OutputStream outputStream = new FileOutputStream(file)) {
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
throw new FileWritingException("Exception while writing file::" + e);
} finally {
IOUtils.closeQuietly(inputStream);
}
FYI, I am getting input stream from implementing sftp java lib jsch as
ChannelSftp channelSftp = setupJsch();
InputStream stream = channelSftp.get(<pathtoFileInExternalFtPLocation>);
public ChannelSftp setupJsch() throws JSchException {
JSch jsch = new JSch();
jsch.setKnownHosts("known-hosts-path");
Session jschSession = jsch.getSession(USERNAME, HOST, PORT);
jschSession.setPassword(PASSWORD);
jschSession.setProxy(new ProxyHTTP(SFTP_PROXY_HOST, SFTP_PROXY_PORT));
Properties config = new Properties();
config.put(STRICT_HOST_KEY_CHECKING_KEY, STRICT_HOST_KEY_CHECKING_VAL);
jschSession.setConfig(config);
jschSession.connect();
return (ChannelSftp) jschSession.openChannel(AMLEFileConstants.SFTP);
}
Jsch: https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html
Thanks to all who replied in comments. Based on your pointers, I just tried another overloading of this get call provided by this API as
void get(<pathtoFileInExternalFtPLocation>, OutputStream <destLocation>)
that drops output stream in destLocation directly into the dest file instead of dealing with Input Stream as a return type. And that worked.https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html#get-java.lang.String-java.io.OutputStream-