阿里云oss上传———通过MultipartFile,springmvc模式

    @PostMapping({"/upload/file"})
    @ResponseBody
    public Result aliUpload(HttpServletRequest httpServletRequest, @RequestParam("file") MultipartFile file) throws URISyntaxException, FileNotFoundException {
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名称通用方法
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        Random r = new Random();
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(r.nextInt(100)).append(suffixName);
        String newFileName = tempName.toString();

        FileOssUtil.uploadFile(newFileName,file);

        Result resultSuccess = ResultGenerator.genSuccessResult();
        resultSuccess.setData("https://yuanmajiazu.oss-cn-hangzhou.aliyuncs.com/" + newFileName);
        return resultSuccess;
    }
package ltd.newbee.mall.controller.common;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class FileOssUtil {

    // 阿里云API的内或外网域名
    private static String endpoint="https://oss-cn-hangzhou.aliyuncs.com";
    
    // 阿里云API的密钥Access Key ID
    private static String accessKeyId="LTAI4Fm6MSmZc9FL2FdhNLP2";
    
    // 阿里云API的密钥Access Key Secret
    private static String accessKeySecret="YE0fugIih7dnPqAwjiXs9tERa12jhD";
    
    // 阿里云API的bucket名称
    private static String bucketName="yuanmajiazu";
    
    // 赋值
//    public static void settingIniOSS(String endpoint, String accessKeyId, String accessKeySecret,String bucketName) {
//        FileBlobUtil.endpoint = endpoint;
//        FileBlobUtil.accessKeyId = accessKeyId;
//        FileBlobUtil.accessKeySecret = accessKeySecret;
//        FileBlobUtil.bucketName = bucketName;
//    }
    
    public static void uploadFile(String objectKey, MultipartFile multipartFile)
                throws OSSException, ClientException, FileNotFoundException {
    
        // 创建OSSClient的实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            if(multipartFile.getSize() != 0 && !"".equals(multipartFile.getName())){
                // getInputStream()返回一个InputStream以从中读取文件的内容。通过此方法就可以获取到流
                InputStream multipartFileInputStream = multipartFile.getInputStream();
                PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, multipartFileInputStream);
                ossClient.putObject(putObjectRequest);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭流
            ossClient.shutdown();
        }
    }
}

 

 

 

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>3.10.2</version>
</dependency>

 

posted @ 2021-06-29 17:00  M号开发者  阅读(520)  评论(0编辑  收藏  举报