简体   繁体   English

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

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

I'm making my own web service using this guide , which is based on a zip code (received from the client) returns an object containing the temperature, city, googlemaps link and a .wav weather report as an byte[] . 我正在使用此指南制作自己的Web服务,该指南基于邮政编码(从客户端接收),返回一个对象,其中包含温度,城市,googlemaps链接和.wav天气报告,以byte[] My web service actually uses another web service to obtain the data added it as it's done here . 实际上,我的Web服务使用另一个Web服务来获取添加到其中的数据,就像在这里所做的那样。 But what my web service actually does is, that it generates the google maps URL and the sound file based on the data. 但是我的网络服务实际上是在根据数据生成google maps URL和声音文件。

The web service successfully adds the city and temperature to the returning object (the client gets the data), but I run into problems when my web service tries to make the bytes array for the sound file. Web服务成功将城市和温度添加到返回的对象(客户端获取数据),但是当我的Web服务尝试为声音文件创建字节数组时,我遇到了问题。

First of all, I've additionally created a new class, which is in the same project and has the same code as the web service class, except that it also has a main function, so I can run it offline to see if the code works. 首先,我另外创建了一个新类,该类与Web服务类在同一项目中,并且具有与Web服务类相同的代码,除了它还具有主要功能之外,因此我可以离线运行它以查看代码是否作品。 I use it to test the code which does not work on the web service. 我用它来测试在Web服务上不起作用的代码。 To be exact, this code works perfectly fine on it's own: 确切地说,此代码本身可以很好地工作:

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

But when the web service runs this code, exceptions occur. 但是,当Web服务运行此代码时,会发生异常。 Firstly, implying the recording.wav is already there (generated with the testing class above) and I'm not generating a new .wav, which is I am trying to make a byte[] from the recording.wav. 首先,暗示record.wav已经存在(使用上面的测试类生成),并且我没有生成新的.wav,这是我试图从recording.wav中创建一个byte []。 In the test class, it says "recording.wav" exists, while when the running web service checks if the file exists, I get a null pointer exception, even though the webservice and the test class are in the same project/folder. 在测试类中,它说“ recording.wav”存在,而正在运行的Web服务检查文件是否存在时,即使Webservice和测试类位于同一项目/文件夹中,我也会得到一个空指针异常。

java.io.FileNotFoundException: recording.wav (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method) java.io.FileNotFoundException:在java.io.FileInputStream.open0(本地方法)上的recording.wav(系统找不到指定的文件)

Secondly, if i try to generate the sound file on the running web service an exception is thrown for this: VoiceManager.getInstance(); 其次,如果我尝试在运行的Web服务上生成声音文件,则会为此抛出一个异常: VoiceManager.getInstance();

Client's side stack trace (copied only the first lines): 客户端的堆栈跟踪(仅复制第一行):

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 Services' side: 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)

I've tried redeploying the web service and client, nothing changed.Now, why is it that these errors occur when running as a web service, while they work perfectly fine running offline? 我已经尝试过重新部署Web服务和客户端,但没有任何变化。现在,为什么在作为Web服务运行时这些错误会发生,而在脱机运行时却能正常工作呢?

To fix the FileNotFoundException you should check what your current working directory is because it most likely differs between your source and test directories. 要修复FileNotFoundException,您应该检查当前的工作目录是什么,因为它在源目录和测试目录之间很可能会有所不同。

Alternatively, you could use Java's ClassLoader to load the file. 另外,您可以使用Java的ClassLoader加载文件。 The ClassLoader works by searching from the project directory regardless of the current working directory. 无论当前的工作目录如何, ClassLoader都是通过从项目目录搜索来工作的。 So ... 所以...

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

Well, pretty sure I found my answer. 好吧,很确定我找到了答案。 The web service uses a different path than normal classes in the project. Web服务使用的路径与项目中普通类的路径不同。 About the java.lang.reflect.InvocationTargetException , the web service also doesn't use the same lib folder as other calsses, I had to copy all my .jar files into the WebContent/WEB-INF/lib folder. 关于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