使用java从网络获取文件和路径 [英] Fetching file and path from network using java

查看:1229
本文介绍了使用java从网络获取文件和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从网络中获取文件,并使用UNC约定保存关联的文件路径。而且我需要在特定的时间晚些时候从该文件导入数据(比如Excel工作表)。任何人都可以建议我一个示例程序,以便我至少可以从网络中获取文件并存储文件路径。

我正在使用一个swing应用程序,需要使用在特定时间触发的调度程序导入Excel表格。早些时候,我使用本地主机Jboss,但现在我需要,如果任何人根据自己的计算机设置一个路径,路径应该以UNC格式存储,以便文件在预定义的时间自动导入到任何其他数据库机。所以基本上我需要根据UNC格式存储路径。我在我的应用程序中使用Swing和Spring



还有一些问题,比如文件夹不可共享,特定时间将如何导入。除此之外,即使共享它将存储整个路径,但我只想要共享它的URL部分。这是我在我的代码中使用:

  java.net.InetAddress i = java.net.InetAddress.getLocalHost(); 
System.out.println(i); //名称和IP地址
System.out.println(i.getHostName()); // name
System.out.println(i.getHostAddress()); //仅IP地址

StringBuffer s = new StringBuffer(filePath);
StringBuffer AfterRemoval = s.delete(0,1);

String finalFilePath;
StringBuffer sb = new StringBuffer(40);
finalFilePath = sb.append(i.getHostName())。append(AfterRemoval).toString();
System.out.println(finalFilePath);


解决方案

没问题。 UNC约定没有驱动器盘符(C :, D :),格式为:
$ b $ p $ \ COMPUTER \ DIR \ DIR \ FILE 。

在Java中,File file = new File(// COMPUTER / DIR / DIR / FILE)或者\ \ \\\\ COMPUTER \ \ DIR \\ DIR \ \ FILE。 (我在反斜杠周围添加了空格)。
$ b $ file.getPath()将总是产生反斜线(Windows)。

对于计算机名称,您可以使用getHostName(),请参阅:
http ://www.rgagnon.com/javadetails/java-0390.html


I need to fetch a file from network, and save the associated file path using UNC convention. And I need to import data from that file (say Excel sheet) later at particular time. Can any one suggest me one sample program so that I can at least fetch the file from network and store the file path.

I am using a swing application and in that I need the excel sheet to get imported using a scheduler which triggers at some particular time. Earlier I was using local host Jboss, but now I need that if any one sets a path according to its own computer, path should be stored in UNC format so that file get imported automatically at the predefined time to the database which is on any other machine. So basically I need to store the path according to UNC format. I am using Swing and spring in my application

There are a few more issues regarding that like if the folder is not sharable how would it will import at particular time. Apart from that if even shared it would store whole of the path, but I want only that portion of url from which it is shared. This is what I use in my code:

java.net.InetAddress i = java.net.InetAddress.getLocalHost();
System.out.println(i);                  // name and IP address
System.out.println(i.getHostName());    // name
System.out.println(i.getHostAddress()); // IP address only

StringBuffer s = new StringBuffer(filePath);
StringBuffer AfterRemoval=s.delete(0,1);

String finalFilePath;
StringBuffer sb = new StringBuffer(40);
finalFilePath = sb.append(i.getHostName()).append(AfterRemoval).toString();
System.out.println(finalFilePath); 

解决方案

No problem. The UNC convention is without drive letters (C:, D:) in the format

\ \ COMPUTER \ DIR \ DIR \ FILE.

In Java suffices File file = new File("//COMPUTER/DIR/DIR/FILE") or "\ \ \ \ COMPUTER \ \ DIR \ \ DIR \ \ FILE". (I added extraneous spaces around the backslashes.)

file.getPath() will always yield backslashes (Windows).

For the computer name you can use getHostName(), see: http://www.rgagnon.com/javadetails/java-0390.html

这篇关于使用java从网络获取文件和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆