Java:如何从servlet上传文件到WebDAV服务器? [英] Java: How to upload a file to a WebDAV server from a servlet?

查看:142
本文介绍了Java:如何从servlet上传文件到WebDAV服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ajax应用程序从用户的浏览器将文件上传到Java应用程序容器。我想做的是:一旦上传完成,我想把文件发送到WebDAV服务器,由主机名(即localhost),端口(即8080)和我想要的位置标识存储文件(即dir1 / dir2)。

my ajax application uploads a file to a Java application container from the user's browser. What I'd like to do is this: once the uploading has completed I want to "send" the file to a WebDAV server, identified by the host name (i.e. localhost), the port (i.e. 8080) and the location where I want to store the file (i.e. dir1/dir2).

我所追求的基本上是一个WebDAV客户端框架,使我能够将文件上传到WebDAV。在我的应用程序中,我已经在使用webdavclient4j,但我似乎找不到用它上传文件的方法?

What I am after is basically a WebDAV client framework that enables me to upload a file to WebDAV. In my application I am already using "webdavclient4j", but I don't seem to find a way to upload a file with it?

有什么想法吗?提前感谢您提供的任何帮助。

Any ideas? Thanks in advance for any help you might provide.

F

推荐答案

使用我最近发布的,易于使用的现代webdav客户端java,Sardine,只需几行代码即可完成。这里有几个例子(第一个使用commons-io来读取文件):

You can do it with only a few lines of code using my recently released and super easy to use modern webdav client for java, Sardine. Here is a couple examples (first one uses commons-io to read the file):

Sardine sardine = SardineFactory.begin("username", "password");
byte[] data = FileUtils.readFileToByteArray(new File("/file/on/disk"));
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", data);

或使用流:

Sardine sardine = SardineFactory.begin("username", "password");
InputStream fis = new FileInputStream(new File("/some/file/on/disk.txt"));
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", fis);

https://github.com/lookfirst/sardine

欢呼,

jon

这篇关于Java:如何从servlet上传文件到WebDAV服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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