Question

I am developing a JAX-WS Web Service to download a file from a SFTP Site. I use the following code to read the file.

channelSftp.cd(sftpHostPath); //sftpHostPath = Path from where file is to be downloaded
inputStream = channelSftp.get(fileName + "." + fileExtn);

which returns an object of java.io.InputStream. This object needs to be returned by the web service as a DataHandler object.

To achieve this, I created a class InputStreamDataSource which implements DataSource and used it to create the object of DataHander as

returnFile = new DataHandler(new InputStreamDataSource(inputStream));

which in turn is returned by the Web Method.

Now, while running the code from SoapUI by enabling MTOM, I get the following output.

SoapUI Output

Please note in the attachment that the file size is 0 and the file name is also not specific.

What is the possible reason for this and a suitable workaround to overcome the problem?

NOTE : The file needs to be returned as DataHandler itself (as an attachment) without using the java.io.File object. Also I want to avoid using any third party jar.

Était-ce utile?

La solution

I was able to solve the issue by creating a DataHandler using byte array.

            buffer = new byte[1024];
            bufferedInputStream = new BufferedInputStream(inputStream);
            byteArrayOutputStream = new ByteArrayOutputStream();
            while( (readCount = bufferedInputStream.read(buffer)) > 0) {
                byteArrayOutputStream.write(buffer, 0, readCount);
            }
            returnFile = new DataHandler(byteArrayOutputStream.toByteArray(), "application/octet-stream");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top