webdav 客户端开发的问题
本人在tomcat下部署了webdav2.1.在进行webdav客户端开发的时候遇到了一些问题,请教各位.
我用的上传代码是:
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpURL;
import org.apache.webdav.lib.WebdavResource;
import java.util.*;
public class CenSlideClientImpl {
public static final boolean putFileToWebDAV(String urlPath,String urlFileName,String uid,String pwd,String localPath,String localFileName)
throws HttpException
{
boolean bool = false;
try
{
HttpURL hrl = new HttpURL(urlPath);
hrl.setUserinfo(uid,pwd);
//hrl.setUser(uid);
//hrl.setPassword(pwd);
WebdavResource wdr = new WebdavResource(hrl);
System.out.println("测试Web路径:" +wdr.getPath());
File file = new File(localPath+"/"+localFileName); //指定上传本地某个目录下的文件
String path = wdr.getPath();
if(!path.endsWith("/"))
path +="/";
path += urlFileName; //上传到WebDAV后可按另外一个文件名保存
System.out.println("\nFilePath is:" +path+"\n");
//if (!wdr.isCollection())
// throw new Exception("Path is currently a file");
//wdr.setPath(currentPath + "/" + urlFileName);
//wdr.setPath("/webdav/down.txt"); //如果指定的文件不存在则发生异常。
try //尝试锁定
{
wdr.setPath(path);
if (wdr.isLocked())
{
// bool = false;
// System.out.println("文件已被锁定,上传失败!");
// return bool;
wdr.unlockMethod();
bool = wdr.putMethod( path,file );
}
else
{
// wdr.lockMethod(uid,10000000);
bool = wdr.putMethod( path,file );
//wdr.unlockMethod();
}
}
catch(Exception ex)
{
bool = wdr.putMethod( path,file );
//wdr.unlockMethod();
System.out.println("\n文件不存在,不需锁定...\n");
}
finally
{
wdr.close();
}
}
catch(MalformedURLException mue)
{
System.out.println("MalformedURLException:"+mue.getMessage());
}
catch(HttpException he)
{
System.out.println("HttpException:"+he.getMessage());
}
catch(IOException ioe)
{
System.out.println("IOException:"+ioe.getMessage());
}
catch(Exception ex)
{
System.out.println("ThrowException:"+ex.getMessage());
}
finally
{
//wdr.close();
}
return bool;
}
public static void main (String args[])
{
boolean bool = false;
try
{
putFileToWebDAV("http://127.0.0.1:9092/slide/files/","remotefile.txt","root","root","C:\\EOS53\\","readme.txt");
}
catch(Exception ex)
{
System.out.println("Exception:"+ex.getMessage());
}
}
}
现在问题是:我在调试时,程序在执行 WebdavResource wdr = new WebdavResource(hrl);
时出现HttpExcetion异常.控制台报:
http-9092-Processor25, 27-二月-2008 15:07:41, unauthenticated, PROPFIND, 400 "Bad Request", 190 ms, /files/
错误.
用户名root的密码都是正常的.http://127.0.0.1:9092/slide/files/这个目录在IE中中访问也正常.就是在java程序中不能正常.
请教各位,这是怎么回事?怎么解决这个问题.