Java skydrive rest文件上载
我尝试用java中的RESTAPI将文件上传到skydrive 这是我的密码:Java skydrive rest文件上载,java,rest,onedrive,Java,Rest,Onedrive,我尝试用java中的RESTAPI将文件上传到skydrive 这是我的密码: public void UploadFile(File upfile) { if (upload_loc == null) { getUploadLocation(); } HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPName
public void UploadFile(File upfile) {
if (upload_loc == null) {
getUploadLocation();
}
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost(upload_loc + "?" + "access_token=" + access_token);
try {
MultipartEntity mpEntity = new MultipartEntity(null,"A300x",null);
ContentBody cbFile = new FileBody(upfile, "multipart/form-data");
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);
System.out.println(post.toString());
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line2 = "";
while ((line2 = rd.readLine()) != null) {
System.out.println(line2);
}
} catch (IOException ex) {
Logger.getLogger(Onlab.class.getName()).log(Level.SEVERE, null, ex);
}
client.getConnectionManager().shutdown();
}
但是,当我尝试运行它时,会出现以下错误:
{
"error": {
"code": "request_body_invalid",
"message": "The request entity body for multipart form-data POST isn't valid. The expected format is:\u000d\u000a--[boundary]\u000d\u000aContent-Disposition: form-data; name=\"file\"; filename=\"[FileName]\"\u000d\u000aContent-Type: application/octet-stream\u000d\u000a[CR][LF]\u000d\u000a[file contents]\u000d\u000a--[boundary]--[CR][LF]"
}
}
我最大的问题是我看不到请求本身。我找不到任何可用的toString方法。我尝试了这种强制边界格式,但我也尝试了空构造函数
我的文件现在是一个包含一些文本的txt文件,我认为边界是主要问题,或者我应该配置更多的参数。当我看到调试模式下的变量时,一切看起来都和msdn中的指南一样
我是rest领域的新手,如果可能的话,我希望将这个apache库与易于使用的HttpClient和HttpPost类保持在一起
提前谢谢,对不起我的英语
编辑:
好的,在长时间的睡眠之后,我决定尝试PUT方法而不是POST。代码运行良好,改动极小:
public void UploadFile(File upfile) {
if (upload_loc == null) {
getUploadLocation();
}
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
String fname=upfile.getName();
HttpPut put= new HttpPut(upload_loc +"/"+fname+ "?" + "access_token=" + access_token);
try {
FileEntity reqEntity=new FileEntity(upfile);
put.setEntity(reqEntity);
HttpResponse response = client.execute(put);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line2 = "";
while ((line2 = rd.readLine()) != null) {
System.out.println(line2);
}
} catch (IOException ex) {
Logger.getLogger(Onlab.class.getName()).log(Level.SEVERE, null, ex);
}
client.getConnectionManager().shutdown();
}
但是第一个问题还没有答案。两件简单的事情:
MultipartEntity
构造函数。在本例中,您将字符集设置为null,这可能不是一个好主意。此外,边界分隔符不够复杂- 姓名:
- 文件名:
- 内容类型:应用程序/八位字节流
ContentBody cbFile = new FileBody(
upfile,
"yourFileNameHere",
"application/octet-stream",
"UTF-8");
你可以使用wireshark来跟踪HTTP请求?不太可能,我现在在一个1000台计算机的网络中,如果我尝试使用过滤器,我会得到200个口袋/秒,但仍然很嘈杂。我试过这个,但很痛苦:)如果你知道远程主机,那应该是一个足够好的过滤器?也许可以试试?我仍然无法解码https口袋:)我尝试了空的多部分构造函数,但现在不是这样工作的。我尝试了更多的体型,但也没有帮助。我想这将是一个非常小的问题,我没有看到。@tg44-你也改变了内容类型吗?请参阅编辑我的答案,这可能也会有所帮助。顺便说一句,我认为mpEntry.addFile可以帮我完成这项工作,但仍然不起作用。如果我在调试模式下检查变量,我会在post变量中看到filename参数。