You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Web应用访问Alfresco开箱即用Web脚本及相关技术问询

Alfresco Integration: Web Scripts & Java API Guidance

Hey there! I’ve worked with Alfresco integrations quite a bit, so let’s walk through your questions clearly and practically.

1. Accessing Alfresco Out-of-the-Box Web Scripts + Sample Code

Alfresco’s out-of-the-box web scripts are RESTful endpoints, typically reachable at the base path http://<your-alfresco-host>:<port>/alfresco/service/. All requests require authentication—Basic Authentication is the most straightforward approach (encode your username:password as Base64 and include it in the Authorization header).

Below are working examples for your required operations, using Java 11+ HttpClient (since you’re familiar with Java):

Prerequisite: Set Up Basic Auth Header

import java.util.Base64;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;

// Encode your Alfresco credentials for Basic Auth
String credentials = "admin:admin"; // Replace with your actual username/password
String authHeader = "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes());

// Initialize HTTP client
HttpClient client = HttpClient.newHttpClient();

Example 1: Create a Folder (Space)

We’ll use the path-based web script to create a folder under a parent directory (e.g., Company Home):

import java.net.URI;
import java.net.http.HttpResponse;

public void createFolder() throws Exception {
    String parentFolderPath = "/Company Home";
    String newFolderName = "My Custom Project Folder";

    // Request body defining the folder
    String requestBody = String.format("{\"name\":\"%s\",\"nodeType\":\"cm:folder\"}", newFolderName);

    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore" + parentFolderPath))
            .header("Authorization", authHeader)
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(requestBody))
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println("Folder Creation Status: " + response.statusCode() + "\nResponse: " + response.body());
}

Example 2: Upload a Document to the New Folder

Use a multipart request to upload a file to the folder you just created:

import java.io.File;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.MultipartBody;

public void uploadDocument() throws Exception {
    String targetFolderPath = "/Company Home/My Custom Project Folder";
    File fileToUpload = new File("/path/to/your/report.pdf");

    // Build multipart request body
    BodyPublisher requestBody = MultipartBody.create()
            .textPart("filedata", fileToUpload.getName())
            .filePart("file", fileToUpload, java.nio.file.Files.probeContentType(fileToUpload.toPath()))
            .build();

    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore" + targetFolderPath + "/children"))
            .header("Authorization", authHeader)
            .POST(requestBody)
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println("Upload Status: " + response.statusCode() + "\nResponse: " + response.body());
}

Pro Tip: You can browse all out-of-the-box web scripts via the Web Scripts Explorer at http://<host>:<port>/alfresco/service/index (requires admin login).


2. Alfresco Java API JARs for Repository Operations

Absolutely! Alfresco offers two main Java API options depending on your use case:

Option 1: Alfresco Content Services Java Client SDK (For External Web Apps)

This is the perfect fit for your custom web app (runs outside Alfresco’s server). It’s a client library that wraps Alfresco’s REST API, so you don’t have to handle raw HTTP requests manually.

Add Maven Dependency (Latest Stable Version)

<dependency>
    <groupId>org.alfresco</groupId>
    <artifactId>alfresco-java-client</artifactId>
    <version>7.4.0</version> <!-- Check for the latest stable version -->
</dependency>

Example: Create a Folder with the SDK

import org.alfresco.core.handler.NodesApi;
import org.alfresco.core.model.NodeBodyCreate;
import org.springframework.web.client.RestTemplate;

public void createFolderWithSDK() {
    // Initialize the API client
    NodesApi nodesApi = new NodesApi(new RestTemplate());
    nodesApi.getApiClient().setBasePath("http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1");
    nodesApi.getApiClient().setUsername("admin");
    nodesApi.getApiClient().setPassword("admin");

    // Define folder details
    NodeBodyCreate folderBody = new NodeBodyCreate();
    folderBody.setName("SDK-Managed Folder");
    folderBody.setNodeType("cm:folder");

    try {
        // Create folder under Company Home (node ID: -my- maps to your user's home, use "companyhome" for global Company Home)
        nodesApi.createNode("-my-", folderBody, null, null, null, null);
        System.out.println("Folder created successfully via SDK!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Option 2: Alfresco Repository Java API (For Server-Side Customizations)

If you plan to deploy custom code inside the Alfresco server (like custom web scripts or AMP modules), you can use the core Repository API. This gives you direct access to Alfresco’s internal services, but it’s not designed for external web apps. You’ll need to pull Alfresco’s internal JARs via their Maven repository.


内容的提问来源于stack exchange,提问作者rishabh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:17:36