作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个网络应用程序,该应用程序使用开放的 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.
来自前端:
关于javascript - 如何调用将文件上传到露天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632602/
我是一名优秀的程序员,十分优秀!