简体   繁体   中英

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[] . My web service actually uses another web service to obtain the data added it as it's done here . But what my web service actually does is, that it generates the google maps URL and the sound file based on the data.

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.

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. I use it to test the code which does not work on the web service. 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. 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. 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.

java.io.FileNotFoundException: recording.wav (The system cannot find the file specified) at java.io.FileInputStream.open0(Native Method)

Secondly, if i try to generate the sound file on the running web service an exception is thrown for this: 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:

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?

To fix the FileNotFoundException you should check what your current working directory is because it most likely differs between your source and test directories.

Alternatively, you could use Java's ClassLoader to load the file. The ClassLoader works by searching from the project directory regardless of the current working directory. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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