java - 将 DataHandler 转换为字符串,并将数据处理程序的内容写入文件

标签 java

我是网络服务的新手。我在 DataHandler 中收到来自客户端的响应。 我必须将数据处理程序的内容写入文件。我还想知道如何从数据处理程序中获取字符串中的数据。

我的程序是

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.";

    }

}

我在文件文件中得到 java.io.BufferedReader@28e70e30 而不是原始内容。 请帮助我。

我的客户端程序是

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

    }

最佳答案

试试这个:

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

关于java - 将 DataHandler 转换为字符串,并将数据处理程序的内容写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965206/

相关文章:

java - 什么时候在 Hibernate 中使用 Spring 中的事务?

java - GridBagLayout 内的 JScrollPane 内的 JPanel 无法正确滚动

java - .NET 的 OS X 端口如何避免 Java 应用程序的非 native GUI 外观?

java - 从字符串解析时间范围

java - 安卓线程问题

java - Hibernate注释@Query参数无法正常工作

java - 在 java 中流式传输后不保存对象属性更改

java - 如何消费Spring Cloud Stream中默认死信队列的消息?

java - 如何为复选框监听器切换正确的类?

java - 是否有用于 PDF/A 的 PDFSmartCopy