rest - 将 .zip 文件上传到 Sharepoint 的 Curl Post 请求返回“压缩(压缩)文件夹在下载时无效
问题描述
我有一个 curl Post 请求,试图将最新文档上传到我们的 Sharepoint。它似乎工作正常,我发送的文件是有效的,并且在我发送之前可以正常打开。一旦它到达共享点,文件大小是相同的,但是当我下载它时,它似乎已损坏并且无法打开。
我已经尝试手动将 zip 文件上传到 Sharepoint,这样可以正常工作。
我还扫描了互联网以寻找解决问题的方法,但我找不到任何东西。
curl -X POST " http://12.3.456.78:8080/TEST-2.0/sharepoint?relativePath=Shared%20Documents%2FRelease%20Documents%2Fv1&teamSite=Test " -H "accept: / " -H "Content-Type: multipart /form-data" -F "file=@Test.zip;type=application/x-zip-compressed"
我希望 sharepoint 上的文件在下载后有效并正确打开,但出现以下错误。
“Windows 无法打开该文件夹。
压缩(压缩)文件夹“C:\Test.zip”无效。
解决方案
答案在代码中,虽然文件正在发送,但显然没有正确发送导致损坏。
我添加的那条有所不同的行如下。
post.setEntity( new FileEntity(file) );
我的完整方法可以在下面找到,如果将来有人需要更多信息,请随时发表评论。
public HttpResponse uploadFile( String teamSite,
String relativePath,
String accessToken,
File file ) throws IOException
{
String fileName = null;
HttpClient client = HttpClientBuilder.create().build();
fileName = file.getName().replaceAll( " ", "%20" );
relativePath = relativePath.replaceAll( " ", "%20" );
teamSite = teamSite.replaceAll( " ", "%20" );
HttpPost post = new HttpPost( URL );
post.setHeader( "Accept", "application/json;odata=verbose" );
post.setHeader( "Authorization", "Bearer " + accessToken );
post.setEntity( new FileEntity(file) ); //This line here
HttpResponse response = client.execute( post );
HttpEntity entity = response.getEntity();
@SuppressWarnings("unused")
String responseString = EntityUtils.toString( entity, "UTF-8" );
return response;
}
推荐阅读
- wordpress - 无法在 wordpress 中设置默认布局
- apache-spark - 如何在pyspark中按列合并多个数据框?
- javascript - 如何在 MysQl 数据库中使用复选框删除一行或多行?
- java - 错误 http/1.1 405 无法使用 Spring Boot 交互 AngularJs 下载文件
- asp.net - 如何避免检索消耗额外时间的不需要的数据
- apache-spark - 在 PySpark 中编写增量表时如何使用 Zorder 聚类?
- sharepoint-2013 - 使用 Sharepoint REST API 查找文件列表
- excel - 避免覆盖刚刚创建的文件
- kubernetes - 与托管在 Kubernetes Pod 中的 Elixir 应用程序连接
- r - 从导入的数据帧逐行迭代向量