2011-01-24 84 views
4

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

我们知道有很多开箱即用的SharePoint网络服务允许将文件上传到SharePoint。但是,我们主要关心的是如果并发用户上传文件会发生什么。例如,我们需要在Java服务器(应用程序服务器)上为每个用户读取一个200 MB文件,然后调用SharePoint发送该数据。即使有5个并发用户,所消耗的内存也将大约为1 GB,并且CPU使用率也会很高。是否有任何建议如何处理服务器内存,在这种情况下文件上传的并发性?

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

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

感谢

+0

为什么会上传文件完全在内存中捕获?它们不会被缓冲,使得大多数数据在给定时间在磁盘上? – earldouglas 2011-02-02 23:06:18

+0

我需要使用SharePoint Web服务(Copy.CopyIntoItems)将文档上载到SharePoint。该方法接受完整文件的文件字节流。或者,请让我知道是否有方法将文件上传到SharePoint逐字节。很可能,我可以使用Java/J2EE应用程序将字节写入磁盘,然后再从该临时磁盘读取文件的另一批作业 - 但这会使该过程复杂化。 – user243542 2011-02-03 20:20:57

回答

0

可能是我错过了什么......但是当你让用户上传文件到您的J2EE服务器,你会不会被第一,然后写上载的内容到一个临时目录将其流式传输到服务器?

当您将缓冲区立即写入磁盘时,您不会遇到有关内存限制的任何问题。

+0

我需要使用SharePoint Web服务(Copy.CopyIntoItems)将文档上载到SharePoint。该方法接受完整文件的文件字节流。或者,请让我知道是否有方法将文件上传到SharePoint逐字节。很可能,我可以使用Java/J2EE应用程序将字节写入磁盘,然后再从该临时磁盘读取文件的另一批作业 - 但这会使该过程复杂化。 – user243542 2011-02-03 20:21:15

1

确定..所以这是我的理解:

  • 您正在尝试使用Sharepoint Copy Service
  • 而该服务需要的SOAP信封被base64encoded流。
  • 由于文件大小是巨大的SOAP请求尺寸变得庞大,需要更多的内存

我能想到的2种选择:

  1. 我不知道很多有关SharePoint,如果可能的话要给文件上传的位置比发送字节,那么你可以ftp/sftp文件到sharepoint服务器,然后用文件的位置调用web服务。

  2. 在Java中,不是使用SOAP消息的开箱即用API,而是编写自定义api。当用户上传文件时,将其保存为base64编码文件。然后你的自定义api将创建一个soap消息并将其流化,而不是将所有内容加载到内存中。

对于选项2:尝试是否可以将文件内容作为肥皂附件发送。如果你想把它作为消息的一部分发送,它会变得有点复杂。

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

1

SharePoint支持用于读取/写入文件的WebDAV协议。

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

0

这里是解决

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 

     } 
    } 
} 
0

或采取对象采取

@Autowired 
ServletContext c; 


      byte[] bytes = file.getBytes(); 

      String UPLOAD_FOLDEdR=c.getRealPath("/images");  
      Path path = Paths.get(UPLOAD_FOLDEdR+"/"+file.getOriginalFilename()); 
      Files.write(path, bytes); 
      String Pic_Name =file.getOriginalFilename() ;