开发者

Get 'Object reference not set to an instance of an object' error when upload file to SharePoint

开发者 https://www.devze.com 2023-04-12 18:57 出处:网络
I use Axis 1.4 to generate java classes from http://10.0.0.22/_vti_bin/Copy.asmx I use following code to upload file to SharePoint site:

I use Axis 1.4 to generate java classes from http://10.0.0.22/_vti_bin/Copy.asmx

I use following code to upload file to SharePoint site:

private String uploadFile(String site, String username, String password, String library, String filename) {
    String tag = "";
    try {
        CopySoapStub stub = SharePointWSDL.newCopy(new URL(site + "/_vti_bin/Copy.asmx"), new CopyLocator());
        stub.setUsername(username);
        stub.setPassword(password);

        String name = (new File(filename)).getName();
        String tagPath = site + "/" + library + "/" + name; 
        FieldInformation[] fis = new FieldInformation[1];
        UnsignedIntHolder uih = new UnsignedIntHolder();
        CopyResultCollectionHolder crch = new CopyResultCollectionHolder();

        fis[0] = new FieldInformation();
        fis[0].setInternalName("Title");
        fis[0].setDisplayName(name);
        fis[0].setType(FieldType.Text);
        fis[0].setValue(name);

        stub.copyIntoItems(null, new String[] { tagPath }, fis, readFile(filename), uih, crch);
        boolean success = true;
        for (int i = 0; i < crch.value.length; i++) {
            if (CopyErrorCode._Success.equals(crch.value[i].getErrorCode().getValue())) continue;
            logger.error(crch.value[i].getErrorCode().getValue() + " : " + crch.value[i].getErrorMessage());
            logger.info(crch.value[i].getDestinationUrl());
            success = false;
        }
        if (success) {
            tag = tagPath;
        }
    } catch (Exception e) {
        logger.error("", e);
    }
    return tag;
}

private byte[] readFile(String filename) {
    File file = new File(filename);
    byte[] tag = new byte[0];
    try {
        tag = new byte[(int)file.length()];
        InputStream is = new FileInputStream(file);     
        int offset = 0;
        int numRead = 0;
        while (offset < tag.length && (numRead = is.read(tag, offset, tag.length - offset)) >= 0) {
            offset += numRead;
        }       
    } catch (Exception e) {
        logger.error("", e);
    }
    return Base64.encode(tag).getBytes();
}

I get 'Object reference not set to an instance of an object' error.

How can I开发者_StackOverflow社区 upload file to SharePoint site using generated classes from Axis 1.4?


My problem is that I use IP in URL instead of domain name. When I use domain name in URL, code works fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号