[英]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.