第一:上传
依赖
com.aliyun.oss aliyun-sdk-oss 2.6.0 com.aliyun aliyun-java-sdk-core 3.3.1 com.aliyun aliyun-java-sdk-dysmsapi 1.0.0-SNAPSHOT ${lib.path}/aliyun-java-sdk-dysmsapi-1.0.0.jar system
Controller
@ApiOperation(value = "上传图片、文件、视频等到阿里云",response=String.class) @RequestMapping(value = "/fileall/upload", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String upload1(@ApiParam(required = true,value = "表单形式提交的文件") @RequestParam MultipartFile file, @ApiParam(required = true,value = "1,2,3") @RequestParam("type") String type){ String image= null; try { image = OSSManageUtil.uploadFile1(file,type); } catch (IOException e) { e.printStackTrace(); } return image; }
工具类OSSManageUtil
private static OSSConfig ossConfig=OSSConfig.getInstance();
/** * 上传图片、文件、视频等到阿里云 * @param file * @param type * @return */ public static String uploadFile1(MultipartFile file, String type) throws IOException { StringBuffer fileName = new StringBuffer(); //获取上传文件的后缀名,如.png、.mp4 等等 String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //文件流 InputStream inputStream =new ByteArrayInputStream(file.getBytes()); if("1".equals(type)){ //指定目录 如 xx111 fileName.append(OssEnum.BUSINESS.getName()); } if("2".equals(type)){ //指定目录 如 xx22 fileName.append(OssEnum.INSTALLER.getName()); } if("3".equals(type)){ //指定目录 如 xx3 fileName.append(OssEnum.USERIMAGE_HEADIMAGE.getName()); } //随机生成文件名 如:指定目录/随机数.后缀名 fileName.append( UUID.randomUUID().toString()+fileExtension); OSSClient ossClient=new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()); //创建上传Object的Metadata ObjectMetadata objectMetadata=new ObjectMetadata(); objectMetadata.setContentLength(inputStream.available()); objectMetadata.setContentEncoding("utf-8"); objectMetadata.setCacheControl("no-cache"); objectMetadata.setHeader("Pragma", "no-cache"); objectMetadata.setContentType(contentType(fileName.substring(fileName.lastIndexOf(".")+1 )));//设置类似 objectMetadata.setContentDisposition("inline;filename=" + fileName);//表示能在线播放 //上传文件 注意fileName如果没有指定目录,则上传到根目录下 ossClient.putObject(ossConfig.getBucketName(), fileName.toString(), inputStream, objectMetadata); //上传后访问地址 return ossConfig.getAccessUrl()+"/" +fileName; }
/** * 根据key删除OSS服务器上的文件 */ public static void deleteFile(OSSConfig ossConfig,String filePath){ OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(),ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()); ossClient.deleteObject(ossConfig.getBucketName(), filePath); }
/** * Description: 判断OSS服务文件上传时文件的contentType * @Version1.0 * @param FilenameExtension 文件后缀 * @return String */
public static String contentType(String FilenameExtension){ if(FilenameExtension.equals("BMP")||FilenameExtension.equals("bmp")){return "image/bmp";} if(FilenameExtension.equals("GIF")||FilenameExtension.equals("gif")){return "image/gif";} if(FilenameExtension.equals("JPEG")||FilenameExtension.equals("jpeg")|| FilenameExtension.equals("JPG")||FilenameExtension.equals("jpg")|| FilenameExtension.equals("PNG")||FilenameExtension.equals("png")){return "image/jpeg";} if(FilenameExtension.equals("HTML")||FilenameExtension.equals("html")){return "text/html";} if(FilenameExtension.equals("TXT")||FilenameExtension.equals("txt")){return "text/plain";} if(FilenameExtension.equals("VSD")||FilenameExtension.equals("vsd")){return "application/vnd.visio";} if(FilenameExtension.equals("PPTX")||FilenameExtension.equals("pptx")|| FilenameExtension.equals("PPT")||FilenameExtension.equals("ppt")){return "application/vnd.ms-powerpoint";} if(FilenameExtension.equals("DOCX")||FilenameExtension.equals("docx")|| FilenameExtension.equals("DOC")||FilenameExtension.equals("doc")){return "application/msword";} if(FilenameExtension.equals("XML")||FilenameExtension.equals("xml")){return "text/xml";} if(FilenameExtension.equals("MP4")||FilenameExtension.equals("mp4")){ return "video/mp4"; } return "text/html"; }
枚举类OssEnum
@Getter public enum OssEnum { BUSINESS("xxx/"),//阿里云中配置的文件目录1 INSTALLER("xxx/"),//文件目录2 USERIMAGE_SHAREQR("xxx/xx/"),//文件目录3 多级目录 private String name; OssEnum(String name) { this.name = name; } }
配置类OSSConfig
@Date public class OSSConfig { private static OSSConfig ossConfig=new OSSConfig(); private static PropertiesLoader loader; private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName; private String accessUrl; private OSSConfig() { loader=new PropertiesLoader("/properties/oss.properties"); this.endpoint=getConfig("aliyun.oss.endpoint"); this.accessKeyId=getConfig("aliyun.oss.accessKeyId"); this.accessKeySecret=getConfig("aliyun.oss.accessKeySecret"); this.bucketName=getConfig("aliyun.oss.bucketName"); this.accessUrl=getConfig("aliyun.oss.accessUrl"); } public static OSSConfig getInstance(){ return ossConfig; } public static String getConfig(String key) { String value = loader.getProperty(key); return value; } }
配置文件resources:properties/oss.properties
aliyun.oss.endpoint=oss-cn-beijing.aliyuncs.com aliyun.oss.accessKeyId=xxx aliyun.oss.accessKeySecret=xxx aliyun.oss.bucketName=xxx ##"https://"+bucketName+endpoint aliyun.oss.accessUrl=xxx
第二:获取某个文件夹下的所有文件
Controller
@ApiOperation(value = "获指定类型的所有图片、文件、视频等",response=String.class) @RequestMapping(value = "/xxx", method = RequestMethod.GET) public ResultgetFile(@ApiParam(required = true,value = "1,2,3") @RequestParam("type") String type){ List
imageList= null; try { imageList = OSSManageUtil.getdFile(type); } catch (Exception e) { e.printStackTrace(); } return new Result(imageList); }
工具类OSSManageUtil
/** * 获取指定目录下的所有文件地址 * @param type * @return */ public static ListgetdFile(String type) { ArrayList imagesList = new ArrayList<>(); if("3".equals(type)){ //获取xxx/xx/目录下的所有文件 ArrayList fileListName = GetFileAllContent(OssEnum.USERIMAGE_HEADIMAGE.getName()); fileListName.stream().forEach(e ->{ //访问地址拼接 imagesList.add(ossConfig.getAccessUrl()+"/"+e); }); } return imagesList; } /** * 获取文件夹下的所有文件名; * @param FileName * @return */ public static ArrayList GetFileAllContent(String FileName) { OSSClient ossClient=new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret()); // 构造ListObjectsRequest请求 ListObjectsRequest listObjectsRequest = new ListObjectsRequest(ossConfig.getBucketName()); //Delimiter 设置为 “/” 时,罗列该文件夹下的文件 listObjectsRequest.setDelimiter("/"); //Prefix 设为某个文件夹名,罗列以此 Prefix 开头的文件 listObjectsRequest.setPrefix(FileName); ObjectListing listing = ossClient.listObjects(listObjectsRequest); ArrayList list = new ArrayList (); // 遍历所有Object:目录下的文件 for (OSSObjectSummary objectSummary : listing.getObjectSummaries()) { String key = objectSummary.getKey(); //过滤指定目录,因为获取指定目录下的文件,不仅把目录下的所有文件都返回,同时返回目录文件 if(key.equals(FileName)){ continue; } list.add(key); } ossClient.shutdown(); return list; }