将文件保存到Web服务 - 文件未显示 - JAXWS

时间:2011-03-11 04:21:20

标签: java jax-ws

我正在尝试编写JAX-WS服务和客户端,客户端可以将特定文件作为文本保存到服务中。为此,我使用了基本的Java IO,但是当我这样做时,它不会创建文本文件,也没有错误。有人可以告诉我这可能是什么原因吗?

非常感谢!

这是我的代码:

服务

@WebService(name = "DemoService1", targetNamespace = "http://service.it")
public class DemoService1 {

@WebMethod(exclude = true)
@PostConstruct
public void initJAXWs() {

}

@WebMethod(exclude = true)
@PreDestroy()
public void destroy() {

}

@WebMethod
public String save(String data){        
    try {
        PrintWriter bw=new PrintWriter(new FileWriter("saving.txt"));
        bw.write(data);
        bw.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "item saved now!";
}

}

客户端

public class DemoClient1 extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 463130394592182427L;
JPanel panel;
JTextArea textArea;
JButton button;

public static String demoServiceURL = "http://192.168.5.113:8090/DemoService1/DemoService1?WSDL";
public static DemoService1 demoService;

/**
 * Connects to the Web Service,Create Binding and return the reference to
 * the service interface
 */

public DemoClient1(){

    setVisible(true);
    setSize(500, 500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    panel = new JPanel();
    panel.setBackground(getBackground().BLACK);
    textArea = new JTextArea(300, 300);
    button = new JButton("save");

    button.addActionListener(this);

    panel.add(button);

    add(textArea, BorderLayout.CENTER);
    add(panel, BorderLayout.SOUTH);
}

public static void connectToDemoWebService() {
    URL baseUrl = DemoService1.class.getResource(".");
    URL serverUrl;
    try {
        serverUrl = new URL(baseUrl, demoServiceURL);
        QName qname = new QName("http://service.it","DemoService1Service");
        DemoService1Service service = new DemoService1Service(
                serverUrl, qname);
        demoService = service.getDemoService1Port();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {

    Runnable r=new Runnable() {

        @Override
        public void run() {
            DemoClient1 dc=new DemoClient1();
        }
    };

    SwingUtilities.invokeLater(r);
}

@Override
public void actionPerformed(ActionEvent arg0) {

    connectToDemoWebService();

    if(arg0.getSource()==button){

        if (demoService != null) {
            System.out.println("enter");
            String text=textArea.getText();
            System.out.println(demoService.save(text));
            System.out.println("exit");
        } else {
            System.out.println("Service null.");
        }

        System.out.println("Server call end");
    }
}

}

0 个答案:

没有答案