首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文件从Java/J2EE应用程序上传到SharePoint

将文件从Java/J2EE应用程序上传到SharePoint
EN

Stack Overflow用户
提问于 2011-01-25 02:24:21
回答 5查看 7.3K关注 0票数 4

我们需要将大文件(可能高达200MB)从Java/J2EE应用程序上传到SharePoint。

我们知道有一些开箱即用的SharePoint web服务可以将文件上传到SharePoint。然而,我们主要关心的是如果并发用户上传文件会发生什么。例如,在调用SharePoint发送数据之前,我们需要为Java服务器(应用服务器)上的每个用户读取一个200MB的文件。即使有5个并发用户,内存消耗也将在1 GB左右,而且CPU使用率也可能很高。在这种情况下,如何处理服务器内存、文件上传的并发性有什么建议吗?

我认为一种选择可能是使用像Flash/Flex这样的技术,它不需要另一个服务器(Java Application server) --然而,想知道如何在J2EE服务器中实现这一点?

http://servername/sitename/_vti_bin/copy.asmx

谢谢

EN

回答 5

Stack Overflow用户

发布于 2011-02-06 09:55:24

好的..。我的理解是这样的:

  • 您正在尝试使用Sharepoint Copy Service
  • And此服务要求流在Soap信封中为base64encoded。
  • 由于文件很大,SOAP请求变得很大,需要更多内存

我能想到两种选择:

  1. 我对sharepoint不太了解,如果可以给出要上传的文件的位置而不是发送字节,那么你可以通过ftp/sftp将文件发送到sharepoint服务器,然后使用文件的位置调用and服务。
  2. 在Java中,为了不使用SOAP消息的开箱即用的api,编写一个自定义的api。当用户上传文件时,将其另存为base64编码文件。然后,您的自定义api将创建一个soap消息并将其流式传输,而不是将所有内容加载到内存中。

对于选项2:如果可以将文件内容作为soap附件发送,请尝试。如果你想把它作为消息的一部分发送,那就有点复杂了。

试一试。我不确定是否有效。

票数 1
EN

Stack Overflow用户

发布于 2011-03-14 17:24:45

SharePoint支持读写文件的WebDAV协议。

您可以使用许多不需要在内存中加载完整文件的WebDAV库。

票数 1
EN

Stack Overflow用户

发布于 2016-05-24 20:25:09

以下是解决方案

代码语言:javascript
复制
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.security.cert.CertificateException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.X509Certificate;
import javax.xml.ws.Holder;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import com.microsoft.schemas.sharepoint.soap.copy.CopyResultCollection;
import com.microsoft.schemas.sharepoint.soap.copy.CopySoap;
import com.microsoft.schemas.sharepoint.soap.copy.DestinationUrlCollection;
import com.microsoft.schemas.sharepoint.soap.copy.FieldInformation;
import com.microsoft.schemas.sharepoint.soap.copy.FieldInformationCollection;
import com.microsoft.schemas.sharepoint.soap.copy.FieldType;

public class Upload {

    private static String username = "yourusrename";

    private static String password = "yourpassword";

    private static String targetPath = "https://www.yoursite.target/filename";

    private static String sourcePath = "file.txt";

    private static String portUrl = "https://www.yoursite.com/_vti_bin/Copy.asmx";

    private static CopySoap soapInstance;

    public static void main(String[] args) {
        activate();
        CopySoap sqs = getInstance();
        String url = targetPath;
        String sourceUrl = sourcePath;
        DestinationUrlCollection urls = new DestinationUrlCollection();
        urls.getString().add(url);
        File file = null;
        byte[] content = null;
        try {
            FileInputStream fileStream = new FileInputStream(file = new File(sourceUrl));

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fileStream.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }

            content = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        FieldInformation titleInfo = new FieldInformation();
        titleInfo.setDisplayName("testpage");
        titleInfo.setType(FieldType.TEXT);
        titleInfo.setValue("Test Page");
        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));
        if (content != null) {
            sqs.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);
        }
    }

    private static void activate() {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CopySoap.class);
        factory.setAddress(portUrl);
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        soapInstance = (CopySoap) factory.create();
        Authenticator.setDefault(new SPAuthenticator());
        Client client = ClientProxy.getClient(soapInstance);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(10000);
        httpClientPolicy.setAllowChunking(false);
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        conduit.setClient(httpClientPolicy);
        TLSClientParameters tcp = new TLSClientParameters();
        tcp.setTrustManagers(new TrustManager[] { (TrustManager) new TrustAllX509TrustManager() });
        conduit.setTlsClientParameters(tcp);
    }

    public static CopySoap getInstance() {
        return soapInstance;
    }

    static class SPAuthenticator extends Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            System.out.println("hitting SP with username and password for " + getRequestingScheme());
            return (new PasswordAuthentication(username, password.toCharArray()));
        }
    }

    /**
     * This class allow any X509 certificates to be used to authenticate the
     * remote side of a secure socket, including self-signed certificates.
     */
    public static class TrustAllX509TrustManager implements X509TrustManager {

        /** Empty array of certificate authority certificates. */
        private static final X509Certificate[] acceptedIssuers = new X509Certificate[] {};

        /**
         * Always trust for client SSL chain peer certificate chain with any
         * authType authentication types.
         * 
         * @param chain
         *            the peer certificate chain.
         * @param authType`enter
         *            code here` the authentication type based on the client
         *            certificate.
         */
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        /**
         * Always trust for server SSL chain peer certificate chain with any
         * authType exchange algorithm types.
         * 
         * @param chain
         *            the peer certificate chain.
         * @param authType
         *            the key exchange algorithm used.
         */
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }

        /**
         * Return an empty array of certificate authority certificates which are
         * trusted for authenticating peers.
         * 
         * @return a empty array of issuer certificates.
         */
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4785597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档