• 文件上传至阿里云


    
    
    public static String uploadFile2OSS(InputStream instream, String fileName) throws IOException {
            String imageName = null;
            OSSClient ossClient = null;
            try {
                ClientConfiguration conf = new ClientConfiguration();
                // 请求超时时间设置
                conf.setConnectionTimeout(5000);
                // 请求失败重试次数
                conf.setMaxErrorRetry(3);
    
                // 创建上传Object的Metadata,该类为阿里云文件上传内部类,可在里面配置一些基本信息,具体参见源码
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setCacheControl("no-cache");
                objectMetadata.setHeader("Pragma", "no-cache");
                objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
                objectMetadata.setContentDisposition("inline;filename=" + fileName);
                // 上传文件
                ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);//ENDPOINT:服务器地址,KEY_ID和KEY_SECERT为阿里云颁发的Access Id/Access Key,用于连接服务器
                PutObjectResult putResult = ossClient.putObject(BUCKET_NAME, fileName, instream, objectMetadata);//BUCKET_NAME为文件上传路径,instream为文件流,
                String ret = putResult.getETag();
                if (!"".equals(ret)) {
                    imageName = fileName;
                }
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
                try {
                    if (instream != null) {
                        instream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
            return imageName;
        }
    
    
    
    
    

    ClientConfiguration客户端配置选项,例如代理设置,用户代理字符串,最大重试次数等。

    OSSClient:阿里云文件上传类,

  • 相关阅读:
    King's Quest
    Prince and Princess
    Strongly connected
    线性渐变--linear-gradient
    镜像渐变-radio-gradient
    宽度自适应-左右
    宽度自适应-左中右
    HTML5 视频规范简介
    SVG格式
    Html5新标签解释及用法
  • 原文地址:https://www.cnblogs.com/sjbas/p/10271332.html
Copyright © 2020-2023  润新知