上传下载oss的工具类-自用
package com.kingdee.eas.base.attachment.common; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.OSSObject; import com.aliyun.oss.model.PutObjectRequest; import com.kingdee.bos.BOSException; import com.kingdee.bos.Context; import com.kingdee.eas.base.attachment.ftp.AttachmentDownloadServer; import com.kingdee.eas.common.EASBizException; import com.kingdee.eas.srt.common.util.ExceptionUtil; import com.kingdee.eas.srt.common.util.ParamUtil; public class OSSClientUtil { private static String endpoint; // 使用刚刚创建的accessKeyId和accessKeySecret private static String accessKeyId; private static String accessKeySecret; private static String bucketName; /** * 上传附件到oss * @param ctx 上下文 * @param path 上传路径 * @param fileName 文件名 * @param content 文件字节流 * @throws EASBizException * @throws BOSException */ public static void uploadOSS(Context ctx,String number,String path,String fileName,byte[] content) throws EASBizException, BOSException { //获取oss参数 dealwithParam(ctx, number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path+fileName, new ByteArrayInputStream(content)); ossClient.putObject(putObjectRequest); ossClient.shutdown(); } /** * 获取oss参数 * @param ctx * @throws EASBizException * @throws BOSException */ public static void dealwithParam(Context ctx,String number) throws EASBizException, BOSException { String param = ParamUtil.getStringParamByNumber(ctx, number);//"OSSPARAM" String[] params = param.split(";"); endpoint = params[0]; accessKeyId = params[1]; accessKeySecret = params[2]; bucketName = params[3]; } /** * 下载OSS文件到本地文件 * @param fileName * @param filePath * @throws BOSException * @throws EASBizException */ public static void downloadOSS(Context ctx,String number,String fileName,String filePath) throws BOSException, EASBizException { //获取oss参数 dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。 ossClient.getObject(new GetObjectRequest(bucketName, fileName), new File(filePath)); ossClient.shutdown(); } /** * 获取文件流 * @param fileName * @return * @throws BOSException * @throws EASBizException */ public static byte[] downloadOSS(Context ctx,String number,String fileName) throws BOSException, EASBizException { //获取oss参数 dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。 OSSObject ossObject = ossClient.getObject(bucketName, fileName); // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。 InputStream input = ossObject.getObjectContent(); byte[] content = null; if (input != null) { AttachmentDownloadServer downServer = new AttachmentDownloadServer(); content = downServer.getContent(input); try{ input.close(); } catch (IOException e) { ExceptionUtil.newEasbizException(e); } } // 关闭OSSClient。 ossClient.shutdown(); return content; } /** * 删除附件 * @param ctx * @param filePath * @return * @throws EASBizException * @throws BOSException */ public static boolean deleteFile(Context ctx,String number,String filePath) throws EASBizException, BOSException { //获取oss参数 dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); boolean exist = ossClient.doesObjectExist(bucketName, filePath); if (!exist) { ExceptionUtil.newEasbizException("文件不存在,filePath="+filePath); return false; } ossClient.deleteObject(bucketName, filePath); ossClient.shutdown(); return true; } /** * 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(".apk") || FilenameExtension.equals(".APK")) { return "application/octet-stream"; } return "text/html"; } }
所使用相关的jar包在个人文件!
package com.kingdee.eas.base.attachment.common;
import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;
import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.model.GetObjectRequest;import com.aliyun.oss.model.OSSObject;import com.aliyun.oss.model.PutObjectRequest;import com.kingdee.bos.BOSException;import com.kingdee.bos.Context;import com.kingdee.eas.base.attachment.ftp.AttachmentDownloadServer;import com.kingdee.eas.common.EASBizException;import com.kingdee.eas.srt.common.util.ExceptionUtil;import com.kingdee.eas.srt.common.util.ParamUtil;
public class OSSClientUtil {private static String endpoint;// = "http://oss-cn-shanghai.aliyuncs.com";// 使用刚刚创建的accessKeyId和accessKeySecretprivate static String accessKeyId;// = "LTAI4GAX7J7m2qFcRnMW82xY";private static String accessKeySecret;// = "ZUktXF46r9ZIicYGcGdnmxsOSqVsCD";private static String bucketName;// = "oss-shanghai-test";/** * 上传附件到oss * @param ctx 上下文 * @param path 上传路径 * @param fileName 文件名 * @param content 文件字节流 * @throws EASBizException * @throws BOSException */public static void uploadOSS(Context ctx,String number,String path,String fileName,byte[] content) throws EASBizException, BOSException {//获取oss参数dealwithParam(ctx, number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path+fileName, new ByteArrayInputStream(content)); ossClient.putObject(putObjectRequest); ossClient.shutdown();}/** * 获取oss参数 * @param ctx * @throws EASBizException * @throws BOSException */public static void dealwithParam(Context ctx,String number) throws EASBizException, BOSException {String param = ParamUtil.getStringParamByNumber(ctx, number);//"OSSPARAM"String[] params = param.split(";");endpoint = params[0];accessKeyId = params[1];accessKeySecret = params[2];bucketName = params[3];}/** * 下载OSS文件到本地文件 * @param fileName * @param filePath * @throws BOSException * @throws EASBizException */public static void downloadOSS(Context ctx,String number,String fileName,String filePath) throws BOSException, EASBizException {//获取oss参数dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。ossClient.getObject(new GetObjectRequest(bucketName, fileName), new File(filePath));
ossClient.shutdown(); }/** * 获取文件流 * @param fileName * @return * @throws BOSException * @throws EASBizException */public static byte[] downloadOSS(Context ctx,String number,String fileName) throws BOSException, EASBizException {//获取oss参数dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。 OSSObject ossObject = ossClient.getObject(bucketName, fileName); // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。 InputStream input = ossObject.getObjectContent(); byte[] content = null; if (input != null) { AttachmentDownloadServer downServer = new AttachmentDownloadServer();content = downServer.getContent(input); try{input.close();} catch (IOException e) {ExceptionUtil.newEasbizException(e);}} // 关闭OSSClient。 ossClient.shutdown(); return content;}/** * 删除附件 * @param ctx * @param filePath * @return * @throws EASBizException * @throws BOSException */public static boolean deleteFile(Context ctx,String number,String filePath) throws EASBizException, BOSException {//获取oss参数dealwithParam(ctx,number); OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); boolean exist = ossClient.doesObjectExist(bucketName, filePath); if (!exist) { ExceptionUtil.newEasbizException("文件不存在,filePath="+filePath); return false; } ossClient.deleteObject(bucketName, filePath); ossClient.shutdown(); return true; } /** * 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(".apk") || FilenameExtension.equals(".APK")) { return "application/octet-stream"; } return "text/html"; } }