gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:25:48 28 4
gpt4 key购买 nike

我正在开发一个网络应用程序,该应用程序使用开放的 Cmis 将文档上传到 Alfresco我有两个 Java 方法来做这项工作,我把它们放在一个 Controller 中,如下所示:

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调用它拜托,我被困住了

最佳答案

org.springframework.web.bind.annotation.RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods.

来自前端:

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

关于javascript - 如何调用将文件上传到露天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632602/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com