阿里云Aliyun OSS图片上传方法

/**
     * 图片上传
     * @param file
     * @return
     * @throws IOException
     * https://blog.csdn.net/softwave
     * 2018-05-21 20:55:38
     */
    public String uploadFileWithoutWatermark(MultipartFile file) throws IOException{
    	FileUtil fileUtil=new FileUtil();
    	InputStream inputStream = file.getInputStream();//MultipartFile转成InputStream
    	String extName=fileUtil.getFileType(file);//文件扩展名
    	OSSClient ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    	String fileName=StringUtil.generatePrimaryKey();
    	// 上传文件流
    	PutObjectResult putObjectResult= ossClient.putObject(BUCKET_NAME, fileName+"."+extName, inputStream);
    	String ret=putObjectResult.getETag();// 关闭client
    	ossClient.shutdown();
    	if(ret.equals("")){
    		return "";
    	}else{
    		return HTTP_HOST+"/"+fileName+"."+extName;
    	}
    }

直接传入Spring Boot的MultipartFile对象来上传文件。

你可能感兴趣的:(Java体系)