I am new to web service. I am getting the response from client in DataHandler. I have to write content of datahandler into a file. And also i want to know how to get data in string from datahandler.
My program is
package com.ws.mtom;
import java.io.File;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
import javax.jws.WebService;
@WebService(endpointInterface = "com.ws.mtom.WSInterface")
public class WSImpl implements WSInterface {
@Override
public String writeFile(DataHandler sTr) {
try {
File file = new File("D:\\xml\\efg.xml");
file.createNewFile();
FileOutputStream fop = new FileOutputStream(file);
sTr.writeTo(fop);
} catch (Exception e) {
return "Fail to write content in file";
}
return "Chutiyap hogaya hai.";
}
}
i am getting java.io.BufferedReader@28e70e30 in file file not the original content. please help me.
my client program is
public static void main(String[] args) throws Exception {
URL wsdlUrl = new URL("http://localhost:8087/ws/fileHandling/?wsdl");
QName qname = new QName("http://mtom.ws.com/", "WSImplService");
Service service = Service.create(wsdlUrl, qname);
DataSource ds = new ByteArrayDataSource(getFileContentAsString("D:\\xml\\abc.xml").getBytes(), "text/plain; charset=UTF-8");
DataHandler handler = new DataHandler(ds);
WSInterface intr = service.getPort(WSInterface.class);
String str = intr.writeFile(handler);
}
Try this :
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
//convert your DataHandler to String
String stringToWrite = IOUtils.toString(sTr.getInputStream, "UTF-8");
//Write String to file
bw.write(stringToWrite);
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