使用 opencmis 从 alfresco 下载文档
Download Document from alfresco using opencmis
我想使用路径从露天下载文档,但我得到的内容长度为空,图像可以在浏览器中下载,但没有显示任何内容。谁能指出我做错了什么。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
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;
public class TestServlet extends HttpServlet {
private static final String ALFRSCO_ATOMPUB_URL = "http://localhost:8484/alfresco/cmisatom";
private static final String REPOSITORY_ID = "cf9aacff-a023-477f-a7e1-a8a901cf0b27";
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
Map<String, String> parameter = new HashMap<String, String>();
// Set the user credentials
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");
// Specify the connection settings
parameter.put(SessionParameter.ATOMPUB_URL, ALFRSCO_ATOMPUB_URL);
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
//Add repository Id
parameter.put(SessionParameter.REPOSITORY_ID, REPOSITORY_ID);
// Create a session
SessionFactory factory = SessionFactoryImpl.newInstance();
Session session = factory.getRepositories(parameter).get(0).createSession();
System.out.println("Connected to repository:" + session.getRepositoryInfo().getName());
Folder root = session.getRootFolder();
System.out.println("Repository Name: "+root.getName()+"id: "+session.getRepositoryInfo().getId());
// (minimal set: name and object type id)
String path1 = "/Hello/img.jpg";
Document doc = (Document) session.getObjectByPath(path1);
System.out.println(doc.getId()+" docname: "+doc.getName());// docId=workspace://SpacesStore/669bd07f-7a3d-471c-b6f3-bff6764f827e
// String fullPath= "/Hello" + "/imgss.png";
Document doc1 = (Document)session.getObject(doc.getId());
// CmisObject obj=doc1;
Document newDocument = (Document) session.getObjectByPath(path1);
System.out.println(newDocument.getId());
//File file = new File(home+"/Downloads/" + fileName + ".txt");
response.setContentType("application/force-download");
//response.setContentLength(-1);
response.setContentType("application/octet-stream");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition","attachment; filename=img.jpg");//fileName);
try {
ContentStream cs = doc1.getContentStream(null);
System.out.println("buffered content: "+cs);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
// FileOutputStream fos = new FileOutputStream(home);
// OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
System.out.println("buffered outputstream: "+in);
DataInputStream din = new DataInputStream(in);
while(din.available() > 0){
out.print(din.readLine());
out.print("\n");
}
din.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
}
}
我在控制台中得到的输出:
Connected to repository:Main Repository
Repository Name: Company Homeid: 4cdc8cd1-ddf1-4e30-95f8-4a2219073580
workspace://SpacesStore/2284cd59-8480-40fa-baed-05d54ddfc561;1.0 docname: imgss.png
workspace://SpacesStore/2284cd59-8480-40fa-baed-05d54ddfc561;1.0
buffered content: ContentStream [filename=imgss.png, length=null, MIME type=image/png, has stream=true][extensions=null]
buffered outputstream: java.io.BufferedInputStream@173b5e2
露天图片:
我下载的图片是:
在户外,您可以使用以下方式下载文件
在我的解决方案中,您将使用
serverUrl : "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom";
username : admin
password : admin
第一种方式:使用文档Id
一个文档的ID就是我们在Alfresco上传的时候Alfresco为这个文档固定的ID
public static void downloadDocumentByID(String serverUrl, String username, String password ,String documentID,String fileName,String destinationFolder){
String fullPath= destinationFolder + fileName;
Document newDocument = (Document) getSession(serverUrl, username, password).getObject(documentID);
System.out.println(newDocument.getId());
try {
ContentStream cs = newDocument.getContentStream(null);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
FileOutputStream fos = new FileOutputStream(fullPath);
OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int n=0;
while ((n=in.read(buf))>0)
{
bufferedOutputStream.write(buf,0,n);
}
bufferedOutputStream.close();
fos.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
第二种方式:使用文件路径
在此解决方案中,您必须获取文档的路径,有时很难获取,这是我始终使用第一个的方式
public static void downloadDocumentByPath(String serverUrl, String username, String password ,String path,String destinationFolder){
String fileExtention = path.substring(path.lastIndexOf(".")+1,path.length());
String folderPath=path.substring(0,path.lastIndexOf("/"));
String fileName=path.substring(path.lastIndexOf("/")+1,path.length());
Folder parentFolder = getFolderByPath(serverUrl, username, password,folderPath);
Document newDocument = getChild(serverUrl, username, password , parentFolder ,fileName);
String fullPath = destinationFolder+fileName;
try {
ContentStream cs = newDocument.getContentStream(null);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
FileOutputStream fos = new FileOutputStream(destinationFolder);
System.out.println("****-**"+destinationFolder+":::");
OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int n=0;
while ((n=in.read(buf))>0)
{
bufferedOutputStream.write(buf,0,n);
}
bufferedOutputStream.close();
fos.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
您可以在此处阅读更多内容
此外,您还可以使用 contentStram 它看起来像这样
public InputStream downloadDocument(String serverURL, String nomUtilisateur, String passwordUtilisateur, String path, String nomFile) {
Document newDocument = (Document) getSession(serverURL, nomUtilisateur, passwordUtilisateur).getObject(path);
ContentStream cs = newDocument.getContentStream(null);
return cs.getStream();
}
并简单地调用这个方法
public void downloaddoc(Document doc) throws FileNotFoundException, TransformerConfigurationException, TransformerException {
InputStream input = downloaddoc(serverUrl, username, password, doc.getPath(), doc.getNameFile);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(doc.getNomRepertoire()), doc.getNomRepertoire()));
}
希望对您有所帮助。
我想使用路径从露天下载文档,但我得到的内容长度为空,图像可以在浏览器中下载,但没有显示任何内容。谁能指出我做错了什么。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
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;
public class TestServlet extends HttpServlet {
private static final String ALFRSCO_ATOMPUB_URL = "http://localhost:8484/alfresco/cmisatom";
private static final String REPOSITORY_ID = "cf9aacff-a023-477f-a7e1-a8a901cf0b27";
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
Map<String, String> parameter = new HashMap<String, String>();
// Set the user credentials
parameter.put(SessionParameter.USER, "admin");
parameter.put(SessionParameter.PASSWORD, "admin");
// Specify the connection settings
parameter.put(SessionParameter.ATOMPUB_URL, ALFRSCO_ATOMPUB_URL);
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
//Add repository Id
parameter.put(SessionParameter.REPOSITORY_ID, REPOSITORY_ID);
// Create a session
SessionFactory factory = SessionFactoryImpl.newInstance();
Session session = factory.getRepositories(parameter).get(0).createSession();
System.out.println("Connected to repository:" + session.getRepositoryInfo().getName());
Folder root = session.getRootFolder();
System.out.println("Repository Name: "+root.getName()+"id: "+session.getRepositoryInfo().getId());
// (minimal set: name and object type id)
String path1 = "/Hello/img.jpg";
Document doc = (Document) session.getObjectByPath(path1);
System.out.println(doc.getId()+" docname: "+doc.getName());// docId=workspace://SpacesStore/669bd07f-7a3d-471c-b6f3-bff6764f827e
// String fullPath= "/Hello" + "/imgss.png";
Document doc1 = (Document)session.getObject(doc.getId());
// CmisObject obj=doc1;
Document newDocument = (Document) session.getObjectByPath(path1);
System.out.println(newDocument.getId());
//File file = new File(home+"/Downloads/" + fileName + ".txt");
response.setContentType("application/force-download");
//response.setContentLength(-1);
response.setContentType("application/octet-stream");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition","attachment; filename=img.jpg");//fileName);
try {
ContentStream cs = doc1.getContentStream(null);
System.out.println("buffered content: "+cs);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
// FileOutputStream fos = new FileOutputStream(home);
// OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
System.out.println("buffered outputstream: "+in);
DataInputStream din = new DataInputStream(in);
while(din.available() > 0){
out.print(din.readLine());
out.print("\n");
}
din.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
}
}
我在控制台中得到的输出:
Connected to repository:Main Repository
Repository Name: Company Homeid: 4cdc8cd1-ddf1-4e30-95f8-4a2219073580
workspace://SpacesStore/2284cd59-8480-40fa-baed-05d54ddfc561;1.0 docname: imgss.png
workspace://SpacesStore/2284cd59-8480-40fa-baed-05d54ddfc561;1.0
buffered content: ContentStream [filename=imgss.png, length=null, MIME type=image/png, has stream=true][extensions=null]
buffered outputstream: java.io.BufferedInputStream@173b5e2
露天图片:
我下载的图片是:
在户外,您可以使用以下方式下载文件
在我的解决方案中,您将使用
serverUrl : "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.0/atom";
username : admin
password : admin
第一种方式:使用文档Id
一个文档的ID就是我们在Alfresco上传的时候Alfresco为这个文档固定的ID
public static void downloadDocumentByID(String serverUrl, String username, String password ,String documentID,String fileName,String destinationFolder){
String fullPath= destinationFolder + fileName;
Document newDocument = (Document) getSession(serverUrl, username, password).getObject(documentID);
System.out.println(newDocument.getId());
try {
ContentStream cs = newDocument.getContentStream(null);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
FileOutputStream fos = new FileOutputStream(fullPath);
OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int n=0;
while ((n=in.read(buf))>0)
{
bufferedOutputStream.write(buf,0,n);
}
bufferedOutputStream.close();
fos.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
第二种方式:使用文件路径
在此解决方案中,您必须获取文档的路径,有时很难获取,这是我始终使用第一个的方式
public static void downloadDocumentByPath(String serverUrl, String username, String password ,String path,String destinationFolder){
String fileExtention = path.substring(path.lastIndexOf(".")+1,path.length());
String folderPath=path.substring(0,path.lastIndexOf("/"));
String fileName=path.substring(path.lastIndexOf("/")+1,path.length());
Folder parentFolder = getFolderByPath(serverUrl, username, password,folderPath);
Document newDocument = getChild(serverUrl, username, password , parentFolder ,fileName);
String fullPath = destinationFolder+fileName;
try {
ContentStream cs = newDocument.getContentStream(null);
BufferedInputStream in =new BufferedInputStream(cs.getStream());
FileOutputStream fos = new FileOutputStream(destinationFolder);
System.out.println("****-**"+destinationFolder+":::");
OutputStream bufferedOutputStream = new BufferedOutputStream(fos);
byte[] buf = new byte[1024];
int n=0;
while ((n=in.read(buf))>0)
{
bufferedOutputStream.write(buf,0,n);
}
bufferedOutputStream.close();
fos.close();
in.close();
}
catch (IOException e)
{
throw new RuntimeException(e.getLocalizedMessage());
}
}
您可以在此处阅读更多内容
此外,您还可以使用 contentStram 它看起来像这样
public InputStream downloadDocument(String serverURL, String nomUtilisateur, String passwordUtilisateur, String path, String nomFile) {
Document newDocument = (Document) getSession(serverURL, nomUtilisateur, passwordUtilisateur).getObject(path);
ContentStream cs = newDocument.getContentStream(null);
return cs.getStream();
}
并简单地调用这个方法
public void downloaddoc(Document doc) throws FileNotFoundException, TransformerConfigurationException, TransformerException {
InputStream input = downloaddoc(serverUrl, username, password, doc.getPath(), doc.getNameFile);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(doc.getNomRepertoire()), doc.getNomRepertoire()));
}
希望对您有所帮助。