有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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服务运行此代码时,会发生异常。 首先,暗示录音。wav已经存在(使用上面的测试类生成),我不会生成新的。wav,我正试图从录音中提取一个字节[]。wav。在测试类中,它表示“recording.wav”存在,而当运行的web服务检查文件是否存在时,我得到一个空指针异常,即使web服务和测试类位于同一个项目/文件夹中

java.io.FileNotFoundException: recording.wav (The system cannot find the file specified) at 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服务运行时发生,而它们在脱机运行时工作得非常好


共 (2) 个答案

  1. # 1 楼答案

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

    或者,您可以使用Java的ClassLoader加载文件。ClassLoader通过从项目目录进行搜索而工作,而不管当前工作目录如何。所以

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

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