访问网络路径中的文件

时间:2013-10-31 12:38:36

标签: java file java-ee filepath

我在网络路径中有一个wav文件,我可以通过\ 192.106.100.35 \记录文件来运行它 从该文件夹我必须访问536.wav文件,我尝试like

File file = new File("file://192.106.100.35/Recorded%20files/536.wav");
    FileInputStream fis;
    byte[] buffer=null;

fis = new FileInputStream(file);
buffer= new byte[fis.available()];
fis.read(buffer);
fis.close();

但是给出了

java.io.FileNotFoundException: file:\192.106.100.35\Recorded%20files\536.wav (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at com.pointel.chart.PlayAudio.doGet(PlayAudio.java:39)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
Oct 31, 2013 5:51:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet PlayAudio threw exception
java.lang.NullPointerException
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:83)
    at com.pointel.chart.PlayAudio.doGet(PlayAudio.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

感谢任何帮助

2 个答案:

答案 0 :(得分:1)

java.io.File仅适用于本地硬盘。使用java.net.URL代替网络资源:

new URL("http://192.106.100.35/Recorded%20files/536.wav");

要使其正常工作,您需要192.106.100.35上的服务器,该服务器支持HTTP协议,并将文件公开为/Recorded%20files/536.wav

其他选项是NFS或SMB(在本地计算机上安装远程文件系统;之后,您可以再次使用File。)

答案 1 :(得分:0)

简短版本是你根本无法做到的。

Javadoc here我猜这可能是一个Windows共享?但是你如何得到它取决于服务器端。如果您可以实际安装远程设备,例如(NFS / Samba / CIFS),那么您可以挽救这种方法。但是,如果它就像一个HTTP服务器,那你就完全咆哮了。