基础URL: http://localhost:8080
/api/thumbnail/generate| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| path | String | 是 | - | 相对文件路径 |
| w | Integer | 否 | 200 | 缩略图宽度(1-4000) |
| h | Integer | 否 | 200 | 缩略图高度(1-4000) |
# 基本用法
curl "http://localhost:8080/api/thumbnail/generate?path=photos/sunset.jpg&w=300&h=300" \
--output thumbnail.jpg
# 使用默认尺寸
curl "http://localhost:8080/api/thumbnail/generate?path=images/photo.png" \
--output thumbnail.jpg
# 生成不同尺寸
curl "http://localhost:8080/api/thumbnail/generate?path=gallery/image.jpg&w=150&h=150" \
--output small_thumbnail.jpg
成功响应:
image/jpegmax-age=3600错误响应:
application/json响应体:
{
"success": false,
"message": "错误信息"
}
| 错误信息 | 原因 | 解决方案 |
|---|---|---|
| "文件路径不能为空" | path参数为空 | 提供有效的文件路径 |
| "宽度必须在1-4000像素之间" | w参数超出范围 | 设置正确的宽度值 |
| "高度必须在1-4000像素之间" | h参数超出范围 | 设置正确的高度值 |
| "源文件不存在" | 文件路径不存在 | 检查文件路径是否正确 |
| "不支持的文件类型" | 文件格式不支持 | 使用支持的图片格式 |
/api/thumbnail/healthcurl "http://localhost:8080/api/thumbnail/health"
{
"success": true,
"message": "服务正常运行",
"data": "OK",
"timestamp": 1699123456789
}
/api/thumbnail/formatscurl "http://localhost:8080/api/thumbnail/formats"
{
"success": true,
"message": "获取支持格式成功",
"data": ["jpg", "jpeg", "png", "webp"],
"timestamp": 1699123456789
}
在 application.properties 中配置文件路径前缀:
# 文件路径前缀
thumbnail.file.prefix=D:/images/
原文件名_宽度_高度.jpgsunset_300_300.jpg// 生成缩略图并显示
async function generateThumbnail(path, width, height) {
try {
const url = `/api/thumbnail/generate?path=${encodeURIComponent(path)}&w=${width}&h=${height}`;
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
const imageUrl = URL.createObjectURL(blob);
// 显示图片
const img = document.createElement('img');
img.src = imageUrl;
document.body.appendChild(img);
return imageUrl;
} else {
const error = await response.json();
console.error('生成失败:', error.message);
}
} catch (error) {
console.error('请求失败:', error);
}
}
// 使用示例
generateThumbnail('photos/sunset.jpg', 300, 300);