Java:如何从 servlet 将文件上传到 WebDAV 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2261507/
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
Java: How to upload a file to a WebDAV server from a servlet?
提问by francescoNemesi
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).
我的 ajax 应用程序从用户的浏览器将文件上传到 Java 应用程序容器。我想做的是:一旦上传完成,我想将文件“发送”到 WebDAV 服务器,由主机名(即 localhost)、端口(即 8080)和我想要的位置标识存储文件(即 dir1/dir2)。
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?
我所追求的基本上是一个 WebDAV 客户端框架,它使我能够将文件上传到 WebDAV。在我的应用程序中,我已经在使用“webdavclient4j”,但我似乎没有找到上传文件的方法?
Any ideas? Thanks in advance for any help you might provide.
有任何想法吗?预先感谢您提供的任何帮助。
F
F
采纳答案by Jon Stevens
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):
您可以使用我最近发布的、超级易用的现代 webdav 客户端 Java 沙丁鱼,只用几行代码就可以做到。这是几个示例(第一个使用 commons-io 读取文件):
Sardine sardine = SardineFactory.begin("username", "password");
byte[] data = FileUtils.readFileToByteArray(new File("/file/on/disk"));
sardine.put("http://yourdavserver.com/adirectory/nameOfFile.jpg", data);
or using streams:
或使用流:
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
https://github.com/lookfirst/sardine
cheers,
干杯,
jon
乔恩
回答by Desintegr
You can use the Hymanrabbit WebDAV Library.
您可以使用Hymanrabbit WebDAV 库。
An example of a WebDAV client to upload content to a WebDAV server (taken from here):
将内容上传到 WebDAV 服务器的 WebDAV 客户端示例(取自此处):
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.Hymanrabbit.webdav.client.methods.PutMethod;
...
// WebDAV URL:
final String baseUrl = ...;
// Source file to upload:
File f = ...;
try{
HttpClient client = new HttpClient();
Credentials creds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(AuthScope.ANY, creds);
PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
RequestEntity requestEntity = new InputStreamRequestEntity(
new FileInputStream(f));
method.setRequestEntity(requestEntity);
client.executeMethod(method);
System.out.println(method.getStatusCode() + " " + method.getStatusText());
}
catch(HttpException ex){
// Handle Exception
}
catch(IOException ex){
// Handle Exception
}