是否可以使用java将文件写入远程目录?
我已将目录映射到我的计算机上,以便我可以通过 Windows 资源管理器浏览和写入该目录。我想通过java写文件。
File f = new File("http://dev1:8080/data/xml/myTestFile123.xml");
f.createNewFile();
我收到以下错误:
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)
有没有办法将文件写入前面有 http:// 的映射目录?因为这就是向我提供目录的方式。它是 oracle 数据库正在创建的虚拟目录。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的理解是,您正在尝试写入 Oracle XML DB存储库。 Oracle XML DB Repository 是 Oracle9i Database Release 2 引入的一项用于 XML 存储的功能,可以通过 FTP 或 HTTP/WebDAV 进行访问。就您而言,您似乎正在尝试使用 HTTP/WebDAV。
正如维基百科 WedDAV 页面中所述:
换句话说,在 WebDAV 存储库中添加文件、删除文件、重命名文件等都是使用 HTTP 单词完成的:PUT、DELETE、MOVE 等(请参阅 RFC 4918 了解更多详细信息)。
因此,可以使用 java.net 中的类来与 WebDAV 服务器进行交互。
或者您可以使用更高级别的 API,例如 Jakarta Commons HttpClient。
或者您可以使用 Java WebDAV 客户端,例如 Slide 项目提供的客户端。这篇文章展示了如何做到这一点,它看起来简单的。然而,由于 Slide 项目现已退役,我不会推荐它。
幸运的是(或者不是),Apache Jackrabbit 项目是 Slide 的替代品......但据我所知 WebDAV 支持Jackrabbit 更关注服务器端实现而不是客户端。无论如何,您可以在此线程中找到一些代码示例jackrabbit 用户邮件列表。
我想我会选择 HttpClient 并使用 教程 或 < a href="http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/" rel="nofollow noreferrer">示例代码作为起点。
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.
As explained in the WedDAV page on Wikipedia:
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 4918 for more details).
Consequently, interacting with a WebDAV server can be done using classes from
java.net
.Or you could use a higher level API like Jakarta Commons HttpClient.
Or you could use a Java WebDAV client like the one provided by the Slide project. This article shows how to do it and it looks simple. However, as the Slide project is now retired, I wouldn't recommend it.
Luckily (or not), the Apache Jackrabbit project is an alternative to Slide... but AFAIK the WebDAV support in Jackrabbit is more focused on server-side implementations than clients. Anyway, you'll find some code samples in this thread on the jackrabbit-users mailing list.
I think I'd choose HttpClient and use the Tutorial or the Sample Code as starting points.
我不太确定我在这里谈论的是什么(不是 Java 人员),但尽管您可能“已映射”,但您传递的是 URL 而不是预期的文件系统路径。例如,如果您在 Windows 下有映射驱动器,请使用分配的驱动器号。
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.
您尝试使用协议传递位置 URI。您需要通过无协议的位置:
Your trying to pass the location URI with a protocol. You need to pass location sans protocol:
不要尝试使用映射的驱动器号(看起来非常弱),请查看 JCIFS:
这段代码展示了如何登录到远程计算机并使用 jCifs 写入文件(归功于 Muneeb Ahmad):
编辑:如添加到原始问题的评论中所述,我的理解是,您现在正在尝试写入 WebDAV 目录。为了更清楚起见,我将在另一个答案中介绍 WebDAV 主题。
Instead of trying to using a mapped drive letter (seems very weak), have a look at JCIFS:
This piece of code shows how to Logon to a Remote Machine and Write File using jCifs (credits to Muneeb Ahmad):
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.
您是如何在 Windows 中映射该文件的?我怀疑它没有使用 HTTP 协议,因为不存在这样的创建文件的机制。所以使用“http”作为协议你不会有任何进展。
找到映射的驱动器号,您可能需要更多类似的信息:
如果您使用 Samba,您可能需要查看 JCIFS 然后你可以使用:
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.
Find the mapped drive letter, you probably want something more like:
If you are using Samba you might want to take a look at JCIFS then you can use:
使用本地路径
如果您可以在 Windows 资源管理器中看到
myTestFile123.xml
,则右键单击它并复制Location:
属性值。然后将其用作 new File() 参数,但可以将反斜杠加倍或将其更改为正斜杠。Use the local path
If you can see
myTestFile123.xml
in windows explorer, then right-click it and copy theLocation:
property value. Then use exactly this as the new File() argument, but either double up the backslashes or change them to forward slashes.