首页 > 解决方案 > 将 .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”无效。

标签: restcurlsharepointuploadzip

解决方案


答案在代码中,虽然文件正在发送,但显然没有正确发送导致损坏。

我添加的那条有所不同的行如下。

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;
}

推荐阅读