gpt4 book ai didi

Java:从远程路径读取文件

转载 作者:行者123 更新时间:2023-12-01 08:53:20 25 4
gpt4 key购买 nike

在Java中,我会将文件从远程路径读取到字符串中。本地点和远程点位于同一网络上。本地点是Windows,远程点是Linux。要从本地路径读取文件,我使用:

filecontent = new Scanner(new File(...local path...)).useDelimiter("\\Z").next();

相反,对于远程路径,我发现了更多不起作用的解决方案,例如:

  1. 文件(..远程路径..)为“\\远程主机\远程目录\..\test.txt”
  2. File(new URI("file:///..远程路径.."));
  3. 驱动器 Z 中的安装路径:并读取为 new File("z:\test.txt");

我找到了另一种解决方案,例如 JCIFS Samba 库,如果没有其他方法,我将使用它。首先,我会问你是否有另一种方法只使用 new File(..)

感谢您的关注。

更新1:

我从 Oracle 数据库获取远程路径。现在我的表中有“z:\test.xml”。使用 new File("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(ClientFatturaElettronicaAruba.java:106:106:

的第 106 行
filebase64 = new Scanner(new File(responsegetparaminviafattura.getFilePath())).useDelimiter("\\Z").next();

更新 2:最小、完整且可验证的示例

我的 Web 应用程序在 Tomcat 8.0 和 Java 1.8 下运行

(参见方法“doGet”)

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中的输出是“文件不存在”。

最佳答案

这里;您的选择3:

Mounting path in drive Z: and read as new File("z:\test.txt");

这肯定有效;但可能你需要

new File("z:\\test.txt");

相反!

您会看到,在最后一个示例中,本地文件之间没有“可见”差异;和一个远程的。对于java来说,它只是某些Windows驱动器上的一个"file"。然后您只需提供一个在 Windows 上有效的文件名即可;并且“\”通常需要转义为“\”!

鉴于您的更新:异常 Java.io.FileNotFoundException: z:\test.xml 非常不言自明 - 该文件不存在。

这就是为什么我建议创建一个真实最小示例;换句话说:尝试运行类似的东西

 public class Test {
public static void main(String[] args) throws Exception {
new File("z:\\test.xml");
}
}

然后看看会发生什么。请注意:至少您更新的问题仍然使用“\”而不是“\”。所以也许:当这些字符串从数据库中出来时,您必须重新处理它们,然后再将它们用作 File() 构造函数的输入!

关于Java:从远程路径读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com