java 是否可以使用java将文件写入远程目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to write files to a remote directory using java?
提问by joe
I have the directory mapped on my machine so that I can browse and write to it via Windows explorer. I would like to write files via java.
我在我的机器上映射了目录,以便我可以通过 Windows 资源管理器浏览和写入它。我想通过java写文件。
File f = new File("http://dev1:8080/data/xml/myTestFile123.xml");
f.createNewFile();
I am getting the following error:
我收到以下错误:
Exception in thread "main" java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at MainTest.createTestFile(MainTest.java:156)
at MainTest.main(MainTest.java:72)
Is there any way to write files to a mapped directory that has the http:// in front? Because thats the way the directory is provided to me. It is a virtual directory that an oracle database is creating.
有没有办法将文件写入前面有 http:// 的映射目录?因为这就是向我提供目录的方式。它是一个 oracle 数据库正在创建的虚拟目录。
采纳答案by Pascal Thivent
My understanding is that you are trying to write to an Oracle XML DB Repository. Oracle XML DB Repository is a feature that has been introduced by Oracle9i Database Release 2 for XML storage and that can be accessed through FTP or HTTP/WebDAV. In your case, it looks like you're trying to use HTTP/WebDAV.
我的理解是您正在尝试写入Oracle XML DB Repository。Oracle XML DB Repository 是 Oracle9i Database Release 2 为 XML 存储引入的一个特性,可以通过 FTP 或 HTTP/WebDAV 访问。在您的情况下,您似乎正在尝试使用 HTTP/WebDAV。
As explained in the WedDAVpage on Wikipedia:
正如维基百科的WedDAV页面所解释的:
WedDAV is a set of extensions on top of HTTP that allows users to edit and manage files collaboratively on remote World Wide Web servers.
WedDAV 是 HTTP 之上的一组扩展,允许用户在远程万维网服务器上协作编辑和管理文件。
In other words, adding files, deleting them, renaming them, etc in a WebDAV repository is done using HTTP words: PUT, DELETE, MOVE, etc (see RFC 4918for more details).
换句话说,在 WebDAV 存储库中添加文件、删除它们、重命名它们等是使用 HTTP 词完成的:PUT、DELETE、MOVE 等(有关更多详细信息,请参阅RFC 4918)。
Consequently, interacting with a WebDAV server can be done using classes from java.net.
因此,可以使用来自java.net.
Or you could use a higher level API like Jakarta Commons HttpClient.
或者您可以使用更高级别的 API,例如Jakarta Commons HttpClient。
Or you could use a Java WebDAV client like the one provided by the Slideproject. This articleshows how to do it and it looks simple. However, as the Slide project is now retired, I wouldn't recommend it.
或者,您可以使用类似于Slide项目提供的 Java WebDAV 客户端。这篇文章展示了如何做到这一点,看起来很简单。但是,由于 Slide 项目现已退役,因此我不推荐它。
Luckily (or not), the Apache Hymanrabbitproject is an alternative to Slide... but AFAIK the WebDAV support in Hymanrabbit is more focused on server-side implementations than clients. Anyway, you'll find some code samples in this threadon the Hymanrabbit-users mailing list.
幸运的是(或不是),Apache Hymanrabbit项目是 Slide 的替代品……但 AFAIK Hymanrabbit 中的 WebDAV 支持更侧重于服务器端实现而不是客户端。无论如何,您会在 Hymanrabbit-users 邮件列表的这个线程中找到一些代码示例。
I think I'd choose HttpClient and use the Tutorialor the Sample Codeas starting points.
回答by Pascal Thivent
Instead of trying to using a mapped drive letter (seems very weak), have a look at JCIFS:
与其尝试使用映射的驱动器号(似乎很弱),不如看看JCIFS:
JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive ...). This client is used extensively in production on large Intranets.
JCIFS 是一个开源客户端库,它在 100% Java 中实现了 CIFS/SMB 网络协议。CIFS 是 Microsoft Windows 平台(例如 Map Network Drive ...)上的标准文件共享协议。此客户端广泛用于大型 Intranet 上的生产。
This piece of code shows how to Logon to a Remote Machine and Write File using jCifs(credits to Muneeb Ahmad):
这段代码展示了如何使用 jCifs 登录到远程机器并写入文件(归功于 Muneeb Ahmad):
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class Logon {
public static void main( String argv[] ) throws Exception {
String user = "user:password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://my_machine_name/D/MyDev/test.txt";
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Muneeb Ahmad".getBytes());
System.out.println("Done");
}
}
Edit: As mentioned in a comment added to the original question, my understanding is now that you are trying to write to a WebDAV directory. I'll cover the WebDAV topic in another answer for more clarity.
编辑:如添加到原始问题的评论中所述,我现在的理解是您正在尝试写入 WebDAV 目录。为了更清楚,我将在另一个答案中介绍 WebDAV 主题。
回答by Andrew Flanagan
I'm not really sure what I'm talking about here (not a Java guy) but although you may "have it mapped" you're passing in a URL instead of an expected file system path. If (for example) you have a mapped drive under Windows, use the drive letter assigned.
我不太确定我在这里谈论的是什么(不是 Java 人),但尽管您可能“已映射”,但您传递的是 URL 而不是预期的文件系统路径。如果(例如)您在 Windows 下有一个映射驱动器,请使用分配的驱动器号。
回答by Greg Buehler
Your trying to pass the location URI with a protocol. You need to pass location sans protocol:
您尝试使用协议传递位置 URI。您需要通过 location sans 协议:
\\dev1\data\xml\myTestFile123.xml
\\dev1\data\xml\myTestFile123.xml
回答by Nosrama
How have you mapped the file in Windows? I suspect it is not using the HTTP protocol, because no such mechanism exists for creating files. So you will not get anywhere using "http" as your protocol.
你是如何在 Windows 中映射文件的?我怀疑它没有使用 HTTP 协议,因为没有这样的机制来创建文件。因此,您将无法使用“http”作为协议。
Find the mapped drive letter, you probably want something more like:
找到映射的驱动器号,你可能想要更像:
File f = new File("F:\dir\file.ext");
If you are using Samba you might want to take a look at JCIFSthen you can use:
如果您正在使用 Samba,您可能想看看JCIFS,然后您可以使用:
smb://server/share/
回答by DigitalRoss
Use the local path
使用本地路径
If you can see myTestFile123.xmlin windows explorer, then right-click it and copy the Location:property value. Then use exactly this as the new File() argument, but either double up the backslashes or change them to forward slashes.
如果您可以myTestFile123.xml在 Windows 资源管理器中看到,则右键单击它并复制Location:属性值。然后完全使用它作为新的 File() 参数,但要么将反斜杠加倍,要么将它们更改为正斜杠。

