繁体   English   中英

Java中的Web服务和客户端(使用Eclipse Apache Axis 2自下而上服务)-某些代码会引发异常

[英]Web service & client in Java (Using Eclipse Apache Axis 2 Bottom Up Service) - certain codes throw exceptions

我正在使用此指南制作自己的Web服务,该指南基于邮政编码(从客户端接收),返回一个对象,其中包含温度,城市,googlemaps链接和.wav天气报告,以byte[] 实际上,我的Web服务使用另一个Web服务来获取添加到其中的数据,就像在这里所做的那样。 但是我的网络服务实际上是在根据数据生成google maps URL和声音文件。

Web服务成功将城市和温度添加到返回的对象(客户端获取数据),但是当我的Web服务尝试为声音文件创建字节数组时,我遇到了问题。

首先,我另外创建了一个新类,该类与Web服务类在同一项目中,并且具有与Web服务类相同的代码,除了它还具有主要功能之外,因此我可以离线运行它以查看代码是否作品。 我用它来测试在Web服务上不起作用的代码。 确切地说,此代码本身可以很好地工作:

public class Sound {

  public static byte[] getSound (String text) throws IOException {
    Voice voice;

    VoiceManager voiceManager = VoiceManager.getInstance();
    voice = voiceManager.getVoice("kevin");
    FreeTTS freeTTS = new FreeTTS(voice);
    freeTTS.setAudioFile("recording.wav");
    freeTTS.startup();
    voice.allocate();      
    voice.speak(text);
    voice.deallocate();
    freeTTS.shutdown();

    File f = new File("recording.wav");

    if (f.exists() && !f.isDirectory()) {
      System.out.println("it exists");
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("recording.wav"));

    int read;
    byte[] buff = new byte[1024];
    while ((read = in.read(buff)) > 0) {
      out.write(buff, 0, read);
    }
    out.flush();
    byte[] audioBytes = out.toByteArray();

    return audioBytes;
  }

  public static void main (String[] args) throws IOException {
    byte[] data = Sound.getSound("Some text");
    System.out.println(data);
  }
}

但是,当Web服务运行此代码时,会发生异常。 首先,暗示record.wav已经存在(使用上面的测试类生成),并且我没有生成新的.wav,这是我试图从recording.wav中创建一个byte []。 在测试类中,它说“ recording.wav”存在,而正在运行的Web服务检查文件是否存在时,即使Webservice和测试类位于同一项目/文件夹中,我也会得到一个空指针异常。

java.io.FileNotFoundException:在java.io.FileInputStream.open0(本地方法)上的recording.wav(系统找不到指定的文件)

其次,如果我尝试在运行的Web服务上生成声音文件,则会为此抛出一个异常: VoiceManager.getInstance();

客户端的堆栈跟踪(仅复制第一行):

org.apache.axis2.AxisFault: com/sun/speech/freetts/VoiceManager
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)

Web服务方面:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)

我已经尝试过重新部署Web服务和客户端,但没有任何变化。现在,为什么在作为Web服务运行时这些错误会发生,而在脱机运行时却能正常工作呢?

要修复FileNotFoundException,您应该检查当前的工作目录是什么,因为它在源目录和测试目录之间很可能会有所不同。

另外,您可以使用Java的ClassLoader加载文件。 无论当前的工作目录如何, ClassLoader都是通过从项目目录搜索来工作的。 所以...

URL url = Sound.class.getClassLoader().getResource("/path/to/recording.wav");
File f = new File(url.toURI());

好吧,很确定我找到了答案。 Web服务使用的路径与项目中普通类的路径不同。 关于java.lang.reflect.InvocationTargetException ,Web服务也没有使用与其他文件夹相同的lib文件夹,我不得不将所有.jar文件复制到WebContent/WEB-INF/lib文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM