在Java中,我想将远程路径中的文件读入字符串。本地点和远程点位于同一网络上。本地点是Windows,远程点是Linux。 要从本地路径读取文件,我使用:
filecontent = new Scanner(new File(...local path...)).useDelimiter("\\Z").next();
相反,对于远程路径,我发现了更多无法解决的解决方案,例如:
我找到了另一个解决方案,比如 JCIFS Samba库,如果没有其他方法,我会使用它。首先,我会问你是否有另一种方法只使用新文件(..)。
感谢您的关注。
更新1:
我从Oracle数据库获取远程路径。现在在我的表格中有" z:\ test.xml"。 使用新文件(" z:\ test.xml"); 堆栈跟踪是:
Java.io.FileNotFoundException: z:\test.xml (Impossibile trovare il percorso specificato)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at it.latraccia.aruba.client.ClientFatturaElettronicaAruba.runClientFatturaElettronicaAruba(ClientFatturaElettronicaAruba.java:106)
at it.traccia.tracciaHub.TracciaHub.doGet(TracciaHub.java:82)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
ClientFatturaElettronicaAruba.runClientFatturaElettronicaAruba第106行(ClientFatturaElettronicaAruba.java:106:
filebase64 = new Scanner(new File(responsegetparaminviafattura.getFilePath())).useDelimiter("\\Z").next();
更新2 :最小,完整且可验证的示例
我的Web应用程序在带有Java 1.8的Tomcat 8.0下运行
(请参阅方法&#34; doGet&#34;)
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Test() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
File f = new File("z:\\test.xml");
if(f.exists()) {
request.setAttribute("ret",f.toString());
}
else {
request.setAttribute("ret","file not exist");
}
visualizzaJsp(request,response,"/index.jsp");
} catch (IOException e) {
request.setAttribute("ret","IOException");
e.printStackTrace();
} catch (ServletException e) {
request.setAttribute("ret","ServletException");
e.printStackTrace();
}
}
public void visualizzaJsp (HttpServletRequest request, HttpServletResponse response,String pathDispatcher)throws IOException, ServletException{
RequestDispatcher dispatcher =this.getServletContext().getRequestDispatcher(pathDispatcher);
dispatcher.forward(request, response);
}
}
我的index.jsp中的输出是&#34; 文件不存在&#34;。
答案 0 :(得分:1)
下面;你的选择3:
Mounting path in drive Z: and read as new File("z:\test.txt");
那应该肯定有用;但可能你需要
new File("z:\\test.txt");
,而不是!
你看,在你的上一个例子中,本地文件之间没有“可见”的区别;和一个遥远的。对于java,它只是某个Windows驱动器上的“文件”。然后你只需提供一个在Windows上有效的文件名;并且“\”通常需要转义为“\”!
鉴于您的更新:异常Java.io.FileNotFoundException: z:\test.xml
非常自我解释 - 该文件不存在。
这就是我建议创建真实最小示例的原因;换句话说:尝试运行类似
的东西 public class Test {
public static void main(String[] args) throws Exception {
new File("z:\\test.xml");
}
}
然后看看会发生什么。请注意:至少您更新的问题仍然使用“\”而不是“\”。所以也许:当这些字符串从数据库出来时,你必须返工,然后再将它们用作File()构造函数的输入!