Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DataHandler to string and also write content of datahandler into file

Tags:

java

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);

    }
like image 418
Komal Rastogi Avatar asked Nov 09 '22 23:11

Komal Rastogi


1 Answers

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);
like image 180
MR67 Avatar answered Nov 14 '22 21:11

MR67