我在strato有一个云存储,也就是hidrive。它使用webdav协议。请注意,它是基于HTTP的。他们提供的client application很差,而且有错误,所以我尝试了各种其他的同步工具,但没有一个能以我需要的方式工作。
因此,我尝试使用Sardine项目在Java语言中实现它。有没有硬拷贝本地源文件夹到外部云文件夹的代码?我还没有在那个方向上找到任何东西。
下面的代码将上传文件...
Sardine sardine = SardineFactory.begin("username", "password");
InputStream fis = new FileInputStream(new File("some/file/test.txt"));
sardine.put("https://webdav.hidrive.strato.com/users/username/Backup", fis);..。但是抛出了一个异常:
Exception in thread "main" com.github.sardine.impl.SardineException: Unexpected response (301 Moved Permanently)
at com.github.sardine.impl.handler.ValidatingResponseHandler.validateResponse(ValidatingResponseHandler.java:48)
at com.github.sardine.impl.handler.VoidResponseHandler.handleResponse(VoidResponseHandler.java:34)
at com.github.sardine.impl.handler.VoidResponseHandler.handleResponse(VoidResponseHandler.java:1)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:218)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160)
at com.github.sardine.impl.SardineImpl.execute(SardineImpl.java:828)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:755)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:738)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:726)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:696)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:689)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:682)
at com.github.sardine.impl.SardineImpl.put(SardineImpl.java:676)打印出该目录中的文件夹,因此连接/身份验证确实成功:
List<DavResource> resources = sardine.list("https://webdav.hidrive.strato.com/users/username/Backup");
for (DavResource res : resources)
{
System.out.println(res);
}请帮助我修复我的代码,或者将我链接到某个为我工作的文件同步库。
发布于 2014-07-04 21:48:10
沙丁鱼使用(内部) HttpClient。这里有类似的问题,你可以找到答案Httpclient 4, error 302. How to redirect?。
发布于 2014-08-21 22:51:15
在调用InputStream ()之前,尝试将put obj转换为字节数组。类似于下面的内容,
byte[] fisByte = IOUtils.toByteArray(fis);
sardine.put("https://webdav.hidrive.strato.com/users/username/Backup", fisByte);这对我很管用。让我知道。
发布于 2014-12-12 14:56:37
我必须扩展"org.apache.http.impl.client.LaxRedirectStrategy“和org.apache.http.impl.client.DefaultRedirectStrategy的getRedirect()方法,以处理所需的方法: PUT、MKOL等。默认情况下,仅重定向GET。
它看起来是这样的:
私有静态最终String[] REDIRECT_METHODS =新String[] { HttpGet.METHOD_NAME,HttpPost.METHOD_NAME,HttpHead.METHOD_NAME,HttpPut.METHOD_NAME,HttpDelete.METHOD_NAME,HttpMkCol.METHOD_NAME };
isRedirectable-方法
for (final String m : REDIRECT_METHODS) {
if (m.equalsIgnoreCase(method)) {
System.out.println("isRedirectable true");
return true;
}
}
return method.equalsIgnoreCase(HttpPropFind.METHOD_NAME);getRedirect-Method:
final URI uri = getLocationURI(request, response, context);
final String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
return new HttpHead(uri);
} else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
return new HttpGet(uri);
} else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
HttpPut httpPut = new HttpPut(uri);
httpPut.setEntity(((HttpEntityEnclosingRequest) request).getEntity());
return httpPut;
} else if (method.equalsIgnoreCase("MKCOL")) {
return new HttpMkCol(uri);
} else if (method.equalsIgnoreCase("DELETE")) {
return new HttpDelete(uri);
} else {
final int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
return RequestBuilder.copy(request).setUri(uri).build();
} else {
return new HttpGet(uri);
}
}这对我很管用。
https://stackoverflow.com/questions/24575356
复制相似问题