【问题标题】:How to call a uploading a file to alfresco如何调用将文件上传到 alfresco
【发布时间】:2018-08-01 11:51:59
【问题描述】:

我正在开发一个使用开放式 Cmis 将文档上传到 Alfresco 的网络应用程序 我有两个 Java 方法在做这项工作,我把它们放在这样的控制器中:

package com.binor.consulting.services;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;

import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;

import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;

import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UploadController {



private Session getSession(String serverUrl, String username,
        String password)
        {

        SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
        Map<String, String> params = new HashMap<String, String>();
        params.put(SessionParameter.USER, username);
        params.put(SessionParameter.PASSWORD, password);
        params.put(SessionParameter.ATOMPUB_URL, serverUrl);
        params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        params.put(SessionParameter.OBJECT_FACTORY_CLASS,
        "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
        List<Repository> repos = sessionFactory.getRepositories(params);
        if (repos.isEmpty()) {
        throw new RuntimeException("Server has no repositories!");
        }
        Repository repo = repos.get(0);
        System.out.println("Found repository: " + repo.getName());
        return repo.createSession();
        }


        @RequestMapping(value="/alf", method = RequestMethod.POST)
        public  void createDocument()

        {
        String serverUrl = "http://localhost:8181/alfresco/api/-default-/public/cmis/versions/1.1/atom";
        String userName = "admin";
        String password = "kader";
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");


        properties.put(PropertyIds.NAME, "Abdelghader");
        properties.put(PropertyIds.CREATED_BY, "admin");
        properties.put("cm:title", "Title x_technical");
        properties.put("cm:description", "Platform_technical_documentation20140601 15M");
        Session session = getSession(serverUrl, userName, password);

        try {
        Folder folder1 = (Folder)session.getObjectByPath("/test1folder");
        VersioningState vs = VersioningState.MAJOR;

        File file = new File("C:\\Users\\kader\\Desktop\\2019_FieldStudy_Research.pdf");
        InputStream fis = new FileInputStream(file);
        DataInputStream dis = new DataInputStream(fis);
        byte[] bytes = new byte[(int) file.length()];
        dis.readFully(bytes);
        ContentStream contentStream = new ContentStreamImpl(file
        .getAbsolutePath(), BigInteger.valueOf(bytes.length), "application/pdf",
        new ByteArrayInputStream(bytes));

        Document newDocument = folder1
        .createDocument(properties, contentStream, vs);

        System.out.println(newDocument.getId());

        } catch (CmisContentAlreadyExistsException ccaee) { 
        System.out.println("ERROR: Unable to Load - CmisContentAlreadyExistsException: " );
        } catch (CmisConstraintException cce) {
        System.out.println("ERROR: Unable to Load - CmisConstraintException: " );
        }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

        }

}

我的问题是我希望能够从我的前端调用这个方法 有什么办法可以用javascript调用它 拜托,我卡住了

【问题讨论】:

    标签: javascript spring spring-restcontroller


    【解决方案1】:

    org.springframework.web.bind.annotation.RequestMapping 注解是 用于将 Web 请求映射到特定的处理程序类和/或处理程序 方法。

    从前端:

    1. 您可以使用 ajax 发送 post 请求。
    2. 您可以使用 html form 元素。

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 2013-02-20
      • 2017-06-05
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多