Java中的Web服务和客户端(使用Eclipse Apache Axis 2 Bottom Up Service) - 某些代码会抛出异常

问题描述 投票:1回答:2

我正在制作自己的网络服务using this guide,它基于邮政编码(从客户端收到)返回一个包含温度,城市,googlemaps链接和.wav天气报告的对象作为byte[]。我的Web服务实际上使用另一个Web服务来获取添加它的数据here。但我的网络服务实际上做的是,它根据数据生成谷歌地图URL和声音文件。

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

首先,我还创建了一个新类,它位于同一个项目中,并且与Web服务类具有相同的代码,除了它还有一个main函数,所以我可以离线运行它以查看代码作品。我用它来测试不适用于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服务运行此代码时,会发生异常。首先,暗示recording.wav已经存在(由上面的测试类生成)并且我没有生成新的.wav,这是我试图从recording.wav创建一个byte []。在测试类中,它表示存在“recording.wav”,而当正在运行的Web服务检查文件是否存在时,即使Web服务和测试类位于同一项目/文件夹中,我也会得到空指针异常。

java.io.FileNotFoundException:recording.wav(系统找不到指定的文件)java.io.FileInputStream.open0(Native Method)

其次,如果我尝试在正在运行的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服务运行时发生,而它们在离线状态下完美运行?

java eclipse web-services axis2
2个回答
1
投票

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

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

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.