阿里云代理上传文件报错com.aliyun.oss - [Client]Unable to execute HTTP request: SocketException(阿里云OSS文件上传教程)

昝阳嘉
2023-12-01

1.依赖

<!--aliyun oss文件上传-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

由于公司使用内网办公,需要设置代理

public static void main(String[] args) throws FileNotFoundException {

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(5000);
        conf.setMaxErrorRetry(3);

        conf.setProxyHost("10.203.73.208");
        conf.setProxyPort(8080);
        OSSClient ossClient=new OSSClient("oss-cn-hangzhou.aliyuncs.com", "LTAI4G6Erc5RCAiLAxdd", "Sxm8dGVUsUDwiOAWUXNkDT1Z1neJbW",conf);
        //上传文件流。
        InputStream inputStream = new FileInputStream("C:\\Users\\520\\Desktop\\spms项目整理.docx");
        ossClient.putObject("gulimall-wangpeng", "spms项目整理.docx", inputStream);
//        // 关闭OSSClient。
        ossClient.shutdown();
        System.out.println("上传成功.");
    }

然后也可以使用测试上传

配置

spring:
  application:
    name: gulimall-third-party
  cloud:
    nacos:
      config:
        namespace: 02bba9cc-3e1c-40b3-a03b-b65a6002d29d
    alicloud:
      access-key: LTAI4G6Erc5RCAiLAExVLnb3
      secret-key: Sxm8dGVUsUDwiOAWUXNkDT1Z1neJbW
      oss:
        config:
          proxyHost: 10.203.73.208
          proxyPort: 8080
        endpoint: http://oss-cn-hangzhou.aliyuncs.com
        bucket: gulimall-wangpeng

 

package com.example.demo.thirdparty;

import com.aliyun.oss.OSSClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@SpringBootTest
public class ThirdPartyApplicationTests {
    @Autowired
    OSSClient ossClient;

    @Test
    public void testUpload() throws FileNotFoundException {
        //上传文件流。
        InputStream inputStream = new FileInputStream("C:\\Users\\520\\Desktop\\Tampermonkey_v4.5.crx");
        ossClient.putObject("gulimall-wangpeng", "Tampermonkey_v4.5.crx", inputStream);
        // 关闭OSSClient。
        ossClient.shutdown();
        System.out.println("上传成功.");
    }
}

 

 类似资料: