远程服务器图片下载

访问远程服务器下载图片并保存在本地的服务器。

package com.nbomb.route.util;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author SunWenKe
 * @version v1.0
 * @date 2023-06-25 11:56
 */
@Component
public class ImageDownLoad {
    @Autowired
    private ServerConfig serverConfig;

    public  AjaxResult extracted(String urls) throws IOException {

        DownloadAndConvertToMultipartFileExample downloadAndConvertToMultipartFileExample = new DownloadAndConvertToMultipartFileExample();
        MultipartFile multipartFile = downloadAndConvertToMultipartFileExample.downloadAndConvertToMultipartFile(urls);

        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        AjaxResult ajax = AjaxResult.success();
        String string = multipartFile.toString();
        // System.out.println("图片------------>"+string);
        String fileName = FileUploadUtils.upload(filePath, multipartFile);
        ajax.put("fileName", fileName);
     
        return ajax;
    }
}

下载方法

package com.nbomb.route.util;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

public class DownloadAndConvertToMultipartFileExample {

    private final OkHttpClient okHttpClient = new OkHttpClient();

    public MultipartFile downloadAndConvertToMultipartFile(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = okHttpClient.newCall(request).execute();

        InputStream inputStream = response.body().byteStream();

        String originalFilename = getOriginalFileName(response);

        return new MockMultipartFile(
                UUID.randomUUID().toString(), // 文件名
                originalFilename, // 显示文件名
                getContentType(response), // 内容类型
                inputStream // 输入流
        );
    }

    private String getOriginalFileName(Response response) {
        // 从响应头中获取文件名
        String contentDispositionHeader = response.header("Content-Disposition");
        if (contentDispositionHeader != null) {
            int indexOfFilename = contentDispositionHeader.indexOf("filename=");
            if (indexOfFilename != -1) {
                return contentDispositionHeader.substring(indexOfFilename + 9)
                        .replaceAll("\"", ""); // 去掉引号
            }
        }

        // 如果响应头中没有文件名,则从 URL 中获取
        int lastSlashIndex = response.request().url().toString().lastIndexOf("/");
        if (lastSlashIndex != -1) {
            return response.request().url().toString().substring(lastSlashIndex + 1);
        }

        // 如果 URL 中也没有文件名,则使用默认值
         String value = String.valueOf(System.currentTimeMillis());
        return value;
    }

    private String getContentType(Response response) {
        String contentTypeHeader = response.header("Content-Type");
        return contentTypeHeader != null ? contentTypeHeader : "application/octet-stream";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值