如何调用将文件上传到露天

时间:2018-08-01 11:51:59

标签: javascript spring spring-restcontroller

我正在使用一个Web应用程序,该应用程序使用开放的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调用吗 拜托,我被卡住了

1 个答案:

答案 0 :(得分:3)

  

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

从前端:

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