网络文件转换为转成MultipartFile

public static InputStream getInputStreamByUrl(String urlStr){
        DataInputStream in = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            in = new DataInputStream(conn.getInputStream());
        } catch (IOException e) {
            System.out.println("url转换输入流失败,错误信息{}"+e.getMessage());
        }
        return in;
    }
public static void main(String[] args) {
        try {
            String url = "https://s3.cn/20230908101719A007.png";
            String name = url.substring(url.lastIndexOf("/")+1);
            // 创建一个MockMultipartFile对象,将输入流和文件名设置为其属性
            MultipartFile imageFile = new MockMultipartFile("file", name, MediaType.APPLICATION_OCTET_STREAM_VALUE, getInputStreamByUrl(url));
            // 获取上传的图片文件名
            String fileName = imageFile.getOriginalFilename();
            // 确定保存图片的路径
            String filePath = "D:/static/image/";
            // 将图片保存到指定位置
            imageFile.transferTo(new File(filePath + fileName));
        } catch (IOException e) {

        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值