I'm facing an issue with a Java SOAP web service that receives an attachment and saves its content to a local file. The problem is that even though the file is being saved, the resulting content is not readable, despite it being a text file.
Here's the code I'm using:
@WebMethod
public String scan(@WebParam(name = "peticion") String peticion,
@WebParam(name = "fichero") DataHandler fichero) throws RemoteException {
String tempFolderPath = "/path/to/my/temporary/directory";
File tempFile = new File(tempFolderPath + File.separator + "temp.txt");
try (InputStream input = fichero.getInputStream();
OutputStream output = new FileOutputStream(tempFile);) {
byte[] b = new byte[100000];
int bytesRead = 0;
while ((bytesRead = input.read(b)) != -1) {
output.write(b, 0, bytesRead);
}
// Additional logic
} catch (Exception e) {
e.printStackTrace();
}
try {
//return getEjb().scan(peticion, fichero);
return "scanned";
} catch (final Exception e) {
LOGGER.error(ERROR_PROCESSING, e);
if (!(e instanceof PeticionException)) {
throw new RemoteException("Error processing the request");
} else {
throw new RemoteException(e.getMessage(), e);
}
}
}
When I send a SOAP request that includes a text file, the file gets saved to the specified temporary directory, but the content is not readable. Can someone help me understand why this is happening and how I can ensure that the file is saved in a readable format?
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://msps/antivirus/ws">
<soapenv:Header/>
<soapenv:Body>
<ws:scan>
<peticion><![CDATA[<peticion xmlns="http://xml.antivirus.msps/Peticion"><acceso><id_aplicacion>APP</id_aplicacion></acceso><fichero><nombre>Prueba.txt</nombre></fichero></peticion>]]></peticion>
<fichero>cid:Prueba.txt</fichero>
</ws:scan>
</soapenv:Body>
</soapenv:Envelope>
Any guidance you can provide would be appreciated. Thanks.