Picgo (https://www.picgo.net/) - 无需注册即可上传图片,注册登录后可更方便管理图片,支持最大25M的图片
首先我们在该网站使用邮箱注册登录
然后,我们可以建立一个独立的相册用于存储不同用途的图片
设置加密(通过相册链接可见)以保证私密性
建立完成之后点击相册右下角的分享按钮可以得到该相册的相册ID,即album_id参数值(通过该参数可在调用API时将图片传入该相册)
之后申请API key(https://www.picgo.net/settings/api)
记住API key后通过阅读API文档(API Version 1.1 | Chevereto V4 Docs)我们就可以很容易的调用该API进行图片存储
接口地址为:https://www.picgo.net/api/1/upload
请求方式为POST
下面为接口测试的演示:
参数分别为(更多参数参考API文档(API Version 1.1 | Chevereto V4 Docs))
请求发送成功后我们可以得到类似下面的json数据返回:
{
"status_code": 200,
"success": {
"message": "file uploaded",
"code": 200
},
"image": {
"name": "332bb79a30172cf002c",
"extension": "jpg",
"size": 2179830,
"width": "2425",
"height": "1364",
"date": "2024-09-06 21:41:37",
"date_gmt": "2024-09-06 13:41:37",
"title": "测试名称",
"description": null,
"nsfw": "0",
"storage_mode": "datefolder",
"md5": "65abf3235d8208ea3a5d2d1613eda64b",
"source_md5": null,
"original_filename": "332.jpg",
"original_exifdata": "{\"FileName\":\"chvtempvjVw7j\",\"FileDateTime\":\"1725630097\",\"FileSize\":\"2179830\",\"FileType\":\"2\",\"MimeType\":\"image\\/jpeg\",\"SectionsFound\":\"ANY_TAG, IFD0\",\"COMPUTED\":{\"html\":\"width=\\\"2425\\\" height=\\\"1364\\\"\",\"Height\":\"1364\",\"Width\":\"2425\",\"IsColor\":\"1\",\"ByteOrderMotorola\":\"0\"},\"Software\":\"www.meitu.com\",\"IPTC\":[],\"width\":\"2425\",\"height\":\"1364\"}",
"views": "0",
"category_id": null,
"chain": "7",
"thumb_size": "8724",
"medium_size": "36490",
"expiration_date_gmt": null,
"likes": "0",
"is_animated": "0",
"is_approved": "1",
"is_360": "0",
"frame_size": "0",
"duration": 0,
"type": "image",
"file": {
"resource": {
"type": "url"
}
},
"id_encoded": "oQogsi",
"filename": "332bb79a30172cf002c.jpg",
"mime": "image/jpeg",
"url": "https://img.picgo.net/2024/09/06/332bb79a30172cf002c.jpg",
"ratio": 1.7778592375366569,
"size_formatted": "2.2 MB",
"url_viewer": "https://www.picgo.net/image/%E6%B5%8B%E8%AF%95%E5%90%8D%E7%A7%B0.oQogsi",
"path_viewer": "/image/%E6%B5%8B%E8%AF%95%E5%90%8D%E7%A7%B0.oQogsi",
"url_short": "https://www.picgo.net/image/oQogsi",
"image": {
"filename": "332bb79a30172cf002c.jpg",
"name": "332bb79a30172cf002c",
"mime": "image/jpeg",
"extension": "jpg",
"url": "https://img.picgo.net/2024/09/06/332bb79a30172cf002c.jpg",
"size": 2179830
},
"thumb": {
"filename": "332bb79a30172cf002c.th.jpg",
"name": "332bb79a30172cf002c.th",
"mime": "image/jpeg",
"extension": "jpg",
"url": "https://img.picgo.net/2024/09/06/332bb79a30172cf002c.th.jpg",
"size": "8724"
},
"medium": {
"filename": "332bb79a30172cf002c.md.jpg",
"name": "332bb79a30172cf002c.md",
"mime": "image/jpeg",
"extension": "jpg",
"url": "https://img.picgo.net/2024/09/06/332bb79a30172cf002c.md.jpg",
"size": "36490"
},
"url_frame": "",
"duration_time": "",
"display_url": "https://img.picgo.net/2024/09/06/332bb79a30172cf002c.md.jpg",
"display_width": "500",
"display_height": 281,
"views_label": "views",
"likes_label": "likes",
"how_long_ago": "3 seconds ago",
"date_fixed_peer": "2024-09-06 13:41:37",
"title_truncated": "测试名称",
"title_truncated_html": "测试名称",
"is_use_loader": false,
"display_title": "测试名称",
"delete_url": "https://www.picgo.net/image/oQogsi/delete/979170d0988e8a03b762a43d31e070829e985647a47bd52b"
},
"status_txt": "OK"
}
返回的数据中拥有上传图片的三种不同清晰程度的访问链接,我们可以根据需求来将获得的url用于自己的项目中 (值得注意的是如果相邻的两次上传同一张照片可能会上传不成功)
下面用java写了一个简单的工具类用于在自己的项目中上传图片,可用于参考
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
public class FileUploadUtil {
// 设置带超时配置的 RestTemplate
private static RestTemplate getRestTemplateWithTimeout() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(10000); // 连接超时时间,单位为毫秒
factory.setReadTimeout(10000); // 读取超时时间,单位为毫秒
return new RestTemplate(factory);
}
public static String uploadFile(MultipartFile file, String fileName) {
try {
RestTemplate restTemplate = getRestTemplateWithTimeout(); // 使用带超时配置的RestTemplate
// 构建请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// 构建请求体
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("key", API_KEY); // API Key
body.add("source", file.getResource()); // 上传文件
body.add("album_id", "你自己的相册ID"); // 相册ID,该参数非必需,没有创建相册可自己修改代码删除
body.add("title", fileName); // 文件名
body.add("format", "json"); // 响应格式
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
// 发送请求
ResponseEntity<Map> responseEntity = restTemplate.exchange(UPLOAD_URL, HttpMethod.POST, requestEntity, Map.class);
// 解析返回结果
Map<String, Object> responseBody = responseEntity.getBody();
if (responseBody != null && responseBody.containsKey("image")) {
Map<String, Object> imageInfo = (Map<String, Object>) responseBody.get("image");
return (String) imageInfo.get("url"); // 返回图片的URL
} else if (responseBody != null && responseBody.containsKey("error")) {
Map<String, Object> errorInfo = (Map<String, Object>) responseBody.get("error");
return "上传失败: " + errorInfo.get("message");
}
return "上传失败";
} catch (Exception e) {
e.printStackTrace();
return "上传失败: " + e.getMessage();
}
}
// API Key 和上传地址常量
private static final String UPLOAD_URL = "https://www.picgo.net/api/1/upload";
private static final String API_KEY = "你自己的API Key";
}
引用测试:
@PostMapping("/upload")
public String upload(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename().trim();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
String prefix = UUID.randomUUID().toString();
String fileName = prefix + suffix;
// 调用工具类上传文件并获取结果
String result = FileUploadUtil.uploadFile(file, fileName);
// 返回上传结果
if (result.startsWith("http")) {
return "result"; // 返回图片URL
} else {
return "error"; // 返回失败信息
}
}
测试结果如下:
访问该链接,可以直接看到我们刚刚上传的图片
通过以上,我们就可以使用图片链接,在个人的小项目中成功的代替OSS图片存储以及图片的引用了