是否可以使用java将文件写入远程目录?

发布于 2024-08-04 03:32:22 字数 615 浏览 11 评论 0原文

我已将目录映射到我的计算机上,以便我可以通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱你是孤单的心事 2024-08-11 03:32:22

我的理解是,您正在尝试写入 Oracle XML DB存储库。 Oracle XML DB Repository 是 Oracle9i Database Release 2 引入的一项用于 XML 存储的功能,可以通过 FTP 或 HTTP/WebDAV 进行访问。就您而言,您似乎正在尝试使用 HTTP/WebDAV。

正如维基百科 WedDAV 页面中所述:

WedDAV 是一组扩展
允许用户编辑的 HTTP 顶部
并协同管理文件
远程万维网服务器。

换句话说,在 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:

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.

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.

近箐 2024-08-11 03:32:22

我不太确定我在这里谈论的是什么(不是 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.

寻找一个思念的角度 2024-08-11 03:32:22

您尝试使用协议传递位置 URI。您需要通过无协议的位置:

\\dev1\data\xml\myTestFile123.xml

Your trying to pass the location URI with a protocol. You need to pass location sans protocol:

\\dev1\data\xml\myTestFile123.xml

百善笑为先 2024-08-11 03:32:22

不要尝试使用映射的驱动器号(看起来非常弱),请查看 JCIFS

JCIFS 是一个开源客户端库,它以 100% Java 实现 CIFS/SMB 网络协议。 CIFS是Microsoft Windows平台上的标准文件共享协议(例如Map Network Drive...)。该客户端广泛用于大型 Intranet 的生产中。

这段代码展示了如何登录到远程计算机并使用 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");
    }
}

编辑:如添加到原始问题的评论中所述,我的理解是,您现在正在尝试写入 WebDAV 目录。为了更清楚起见,我将在另一个答案中介绍 WebDAV 主题。

Instead of trying to using a mapped drive letter (seems very weak), have a look at 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.

This piece of code shows how to Logon to a Remote Machine and Write File using jCifs (credits to 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.

迟到的我 2024-08-11 03:32:22

您是如何在 Windows 中映射该文件的?我怀疑它没有使用 HTTP 协议,因为不存在这样的创建文件的机制。所以使用“http”作为协议你不会有任何进展。

找到映射的驱动器号,您可能需要更多类似的信息:

File f = new File("F:\\dir\\file.ext");

如果您使用 Samba,您可能需要查看 JCIFS 然后你可以使用:

smb://server/share/

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:

File f = new File("F:\\dir\\file.ext");

If you are using Samba you might want to take a look at JCIFS then you can use:

smb://server/share/
人间☆小暴躁 2024-08-11 03:32:22

使用本地路径

如果您可以在 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文