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

Spring Boot调用Alfresco Transform Core AIO实现图片缩略图生成

代码实现Alfresco Transform Core AIO生成图片缩略图

方案一:调用Transform Core的REST API(推荐)

直接调用已部署容器的API是最稳定的方式,无需依赖本地transform-core包,可避免版本冲突问题。

Java代码示例(使用RestTemplate)

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AlfrescoImageTransformer {
    private static final String TRANSFORM_API_URL = "http://localhost:8088/transform";

    public static void generateThumbnail(String sourceImagePath, String outputThumbnailPath, int width, int height) throws IOException {
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 构建请求体
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", new FileSystemResource(new File(sourceImagePath)));
        body.add("targetMimeType", "image/png"); // 可替换为image/jpeg等格式
        body.add("resizeWidth", String.valueOf(width));
        body.add("resizeHeight", String.valueOf(height));
        // 如需保持宽高比,只设置width或height,另一个参数传"-1"即可

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        // 发送请求并处理响应
        ResponseEntity<byte[]> response = restTemplate.postForEntity(TRANSFORM_API_URL, requestEntity, byte[].class);

        if (response.getStatusCode().is2xxSuccessful()) {
            Path outputPath = Paths.get(outputThumbnailPath);
            Files.write(outputPath, response.getBody());
            System.out.println("缩略图生成成功:" + outputThumbnailPath);
        } else {
            throw new IOException("转换失败,状态码:" + response.getStatusCode());
        }
    }

    public static void main(String[] args) throws IOException {
        generateThumbnail("source.jpg", "thumbnail.png", 100, 100);
    }
}

参数说明

  • resizeWidth/resizeHeight:设置目标尺寸,保持比例时仅需设置其中一个,另一个传-1
  • targetMimeType:指定输出图片格式,支持image/jpeg、image/png等常见格式

方案二:修复本地transform-core依赖使用问题

若需使用本地依赖,需确保依赖配置完整,且版本与容器一致。

Maven依赖配置

<dependencies>
    <dependency>
        <groupId>org.alfresco</groupId>
        <artifactId>alfresco-transform-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <!-- 引入图片转换核心组件 -->
    <dependency>
        <groupId>org.alfresco</groupId>
        <artifactId>alfresco-transformer-image</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>

本地转换代码示例

import org.alfresco.transformer.image.ImageTransformer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class LocalImageTransformer {
    public static void generateThumbnailLocal(String sourcePath, String outputPath, int width, int height) throws IOException {
        ImageTransformer imageTransformer = new ImageTransformer();
        
        // 设置转换参数
        Map<String, String> transformOptions = new HashMap<>();
        transformOptions.put("resizeWidth", String.valueOf(width));
        transformOptions.put("resizeHeight", String.valueOf(height));
        transformOptions.put("targetMimeType", "image/png");

        File sourceFile = new File(sourcePath);
        File outputFile = new File(outputPath);

        // 执行转换
        try (FileInputStream inputStream = new FileInputStream(sourceFile);
             FileOutputStream outputStream = new FileOutputStream(outputFile)) {
            imageTransformer.transform(inputStream, outputStream, sourceFile.length(), 
                sourceFile.getName(), "image/jpeg", transformOptions, null);
            System.out.println("本地转换缩略图成功:" + outputPath);
        } catch (Exception e) {
            throw new IOException("本地转换失败", e);
        }
    }

    public static void main(String[] args) throws IOException {
        generateThumbnailLocal("source.jpg", "local-thumbnail.png", 150, 150);
    }
}

注意事项

  • 本地依赖需保证所有转换组件都已引入,否则会出现找不到转换器的错误
  • 依赖版本必须与部署的Transform Core AIO容器版本(2.3.0)一致,避免兼容性问题

内容的提问来源于stack exchange,提问作者saber tabatabaee yazdi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 13:52:38