liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Public class convertImageURLToFile {
    public static Blob downloadImage(String imageUrl) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(imageUrl);
        request.setMethod('GET');
 
        HttpResponse response = http.send(request);
 
        if (response.getStatusCode() == 200) {
            return response.getBodyAsBlob();
        } else {
            System.debug('Unable to download image. Status code: ' + response.getStatusCode());
            return null;
        }
    }
 
 
    public static String saveImageToAttachment(Blob imageBlob, String fileName) {
        Attachment attachment = new Attachment();
        attachment.Name = fileName;
        attachment.Body = imageBlob;
        attachment.ParentId = 'a3TC500000003dqMAA'; // 将文件关联到特定记录
        insert attachment;
 
        return '/sfc/servlet.shepherd/version/download/' + attachment.Id;
    }
}