【问题标题】:Gettting 400 Bad Request while uploading file to SharePoint 2010 using Copy.CopyIntoItems web service使用 Copy.CopyIntoItems Web 服务将文件上传到 SharePoint 2010 时收到 400 错误请求
【发布时间】:2012-02-14 17:10:14
【问题描述】:

SharePoint 新手。

我正在尝试使用 Java 的 CopyIntoItems Web 服务方法将文档上传到 SharePoint,但不断收到 400 Bad Request。我使用 Java 的 wsimport 从 .wsdl 文件生成类文件。这是我生成的类的 Java 代码。

public static void createDocument(CopySoap port) {
    String url = SoapPortProvider.spSiteUrl + "/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "http://null";

    byte[] content = IoUtil.getBytes(new File("C:/CopyFile/READ-ME.txt"));

    FieldInformation descInfo = new FieldInformation ();
    descInfo.setDisplayName("Test Doc");
    descInfo.setType(FieldType.TEXT);
    descInfo.setValue("Test uploaded file");        


    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(descInfo);  

    CopyResultCollection results = new CopyResultCollection ();
    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}

我的 SOAP 请求看起来像

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">    
         <SourceUrl>http://null</SourceUrl>
         <DestinationUrls>
            <string>https://www.mysite.com/sites/TestSite/Shared Documents/Temp Folder/test.txt</string>
         </DestinationUrls>
         <Fields>
            <FieldInformation Value="Test uploaded file" DisplayName="Test Doc" Type="Text"/>
         </Fields>
         <Stream>KioqTWFrZSBzdXJlIHRoZSBjb250ZW50IGlzIHVuZGVyIEM6L0NvcHlGaWxlLy4gICoqKg0KDQpUbyBydW46DQoNCjEuICBFZGl0IHRoZSBkZXBsb3kucHJvcHMgZmlsZS4gIFNwZWNpZnkgdGhlIHNvdXJjZSAoZm9sZGVyIHRoYXQgY29udGFpbiBpdGVtcyB0byBkZXBsb3kpLCBkZXN0aW5hdGlvbiAoZm9sZGVyIHRvIGRlcGxveSB0byksIGFuZCBmaWxlcyAodGhlIGl0ZW1zIHRvIGRlcGxveSkuDQoyLiAgRG91YmxlIGNsaWNrIG9uIGRlcGxveUN1c3RvbWl6YXRpb24uYmF0DQoNCk5vdGUgdGhhdCBhIGxvZyBvZiB0aGUgcHJvZ3Jlc3Mgd2lsbCBiZSBjcmVhdGVkIGluIEM6L0NvcHlGaWxlL2NvcHlGaWxlLmxvZyANCg0KTm90ZSB0aGF0LCBmb3IgcHJlY2F1dGlvbiwgZXhpc3RpbmcgZmlsZSB3aWxsIG5vdCBiZSBvdmVyd3JpdHRlbiwgaW5zdGVhZCB3aWxsIGJlIHNhdmVkIGFzIHlvdXJfY29kZV9maWxlLm9sZA==</Stream>
      </CopyIntoItems>
    </S:Body>
</S:Envelope>

我得到的回应是

null: HTTP/1.1 400 Bad Request
Content-length: 0
X-powered-by: ASP.NET
Server: Microsoft-IIS/7.5
Date: Tue, 14 Feb 2012 16:29:51 GMT
Microsoftsharepointteamservices: 14.0.0.5138

这并没有告诉我太多。可能缺少什么?

【问题讨论】:

  • http://null 的 sourceUrl 看起来很可疑。在这个答案中,看起来将源设置为与目标相同的 URL。 stackoverflow.com/a/998565/1030409
  • 我也尝试过,但仍然得到相同的响应。不过还是谢谢。
  • 我需要相同的解决方案。尝试以下答案并获得大量编译错误。请帮忙

标签: java web-services sharepoint


【解决方案1】:

我已经使用了以下代码,它对我来说非常适合:

    try {
        //Copy WebService Settings 
        string webUrl           = "http://sharepointportal.ABC.com/";
        WSCopy.Copy copyService = new WSCopy.Copy();
        copyService.Url         = webUrl + "/_vti_bin/copy.asmx";
        copyService.Credentials = new NetworkCredential("username", "****", "Domain");

        //Declare and initiates the Copy WebService members for uploading 

        string sourceUrl        = "C:\\Work\\Ticket.Doc";   

        //Change file name if not exist then create new one     
        string[] destinationUrl    = { "http://sharepointportal.ABC.com/personal/username/Document Upload/Testing Document/newUpload.Doc" };

        WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();

        WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();

        WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();

        fFiledInfo.DisplayName = "Description";

        fFiledInfo.Type        = WSCopy.FieldType.Text;

        fFiledInfo.Value       = "Ticket";

        WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo }; 

        FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read); 

        byte[] fileContents = new Byte[strm.Length]; 

        byte[] r = new Byte[strm.Length];

        int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
        strm.Close();
        //Copy the document from Local to SharePoint 

        uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray); 

        MessageBox.Show("Suceess");  

     }
     catch (Exception ex) { 
        MessageBox.Show(ex.Message);

     }

【讨论】:

  • 嗨,拉维,你能发布你的 SOAP 请求吗?我认为我的 SOAP 消息有问题,也许我可以通过与您的比较来识别它。
  • 没关系,我的代码中有一个错误,我在初始化复制服务时传递了额外的字符串。谢谢拉维。
【解决方案2】:

知道了!我的代码在初始化时只有一个错误。这是任何想要使用 SharePoint 和 Java 的人的工作代码。我使用 JAX-WS wsimport 工具从 .wsdl 文件生成类文件。您可以将工具直接指向 WSDL 的 url,例如,https://my.site.come/sites/mysite/_vti_bin/copy.asmx?wsdl

public static CopySoap getPort(String username, String password)  {

    Copy service = new Copy();
    CopySoap port = service.getCopySoap();

    BindingProvider bp = (BindingProvider) port;

    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
            "https://my.site.com/sites/mysite/_vti_bin/copy.asmx");


    return port;
}   

public static void createDocument(CopySoap port) {
    String url = "https://my.site.com/sites/mysite/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "C:\\CopyFile\\READ-ME.txt";     

    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    byte[] content = IoUtil.getBytes(new File(sourceUrl));

    FieldInformation titleInfo = new FieldInformation ();
    titleInfo.setDisplayName("Title");
    titleInfo.setType(FieldType.TEXT);
    titleInfo.setValue("Test Doc");

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(titleInfo);

    CopyResultCollection results = new CopyResultCollection ();

    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);      

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));       

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}

【讨论】:

  • 我只想说这篇文章对我帮助很大!我一直在努力让它工作大约一天。
  • 很高兴它有帮助。不过只是一个警告。我在将大文件 (>10MB) 上传到 SharePoint 时遇到问题,它非常慢。我最终将 WebDav 与 Web 服务一起使用。基本上将驱动器映射到 SharePoint,然后使用 Java IO 将文件复制到该驱动器。上传速度非常快。
  • 我没有遇到这个问题,我上传了一个 28MB 的文件,它的上传时间与一个较小的 4MB 文件差不多。当然,我们的 SharePoint 位于站点上,这可能是原因。我们正在切换到一个位于不同状态的 SharePoint,所以我会记住这一点。再次感谢。 =)
  • 我可以得到你的完整的带有导入的 java 文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多