文件上传示例(上传到amazon s3服务器)

文件上传示例(上传到amazon s3服务器)

action调用上传工具类

@Action(value = "files-upload",
            results = {@Result(name = SUCCESS, location = COMMON_PAGES_ROOT + "/noData.jsp")})
    public String filesUpload() {
        try {
//           拦截器生成的临时文件
            baseLog.info("===start to upload:"+uploadFile.getName());
            ......
            if (StringUtils.isEmpty(upload(FILE_ACTION_URL))) {
                setStatus(ERROR_CODE_SYSTEM);
                return SUCCESS;
            }
            JSONArray array = JSONArray.fromObject(upload(FILE_ACTION_URL));
            List<TwitterFile> allFiles = new ArrayList<>();
            for (Object o : array) {
                JSONObject json = JSONObject.fromObject(o);
                if (json != null) {
                    TwitterFile allFile = jsonToFiles(json.optString("url"), uploadFile.length());
                    allFiles.add(allFile);
                    break;
                }
            }
            ......

FILE_ACTION_URL 是本地临时存放的相对路径,如: FILE_ACTION_URL= /upload/file

upload方法

  //上传到服务器-》上传到s3
    private String upload(String url) throws NameServiceException {     //  url=/upload/file
        baseLog.info("file name =" + uploadFileName + ", contentType=" + uploadContentType);
        NameService nameService = NameServiceBuilder.getInstance().getNameService();
        StringBuilder sb = new StringBuilder();
        sb.append(nameService.getHost(UPLOAD_SERVICE_NAME)).append(url); //  sb=http://192.168.0.121:8011/file-upload/upload/file
        baseLog.info("================> " + sb.toString());    //  ================> http://192.168.0.121:8011/file-upload/upload/file
        Map<String, String> params = new HashMap<String, String>(1);
        params.put(FileUploader.PRI_PATH_REQUEST_PARAM_KEY, pageTenantId.toString());   //查询条件参数

        String uploadResponse = new FileUploader(sb.toString()).upload(uploadFileName, uploadContentType, uploadFile, params);
          /** FileUploader工具类上传文件到
          s3文件 [{"name":"通讯录拼音处理方式.txt","url":"https://devrs.s3.cn-north-1.amazonaws.com.cn/12228/2017/05/13/451027d0-22f0-446e-99bd-a6753dac63d9.txt","suffix":"txt"}]*/
        baseLog.info("Response ================> " + uploadResponse);
        return uploadResponse;
    }

工具类上传s3

public class FileUploader {

    private static final Logger LOG = LoggerFactory.getLogger(FileUploader.class);
    private static final int CONNECTION_TIMEOUT_FIRST = 5000;
    private static final int CONNECTION_TIMEOUT_SECOND = 50000;
    private static final String REMOVE_REQUEST_PARAM_KEY = "rm";
    public static final String PRI_PATH_REQUEST_PARAM_KEY = "pripath";

    private static final String FILE_ACTION_URL = "/upload/file";
    private static final String IMAGE_ACTION_URL = "/upload/image";
    private static final String ICON_ACTION_URL = "/upload/icon";
    /**
     * 上传的servlet url
     */
    private final String uploadServlet;

    public String getUploadServlet() {
        return uploadServlet;
    }

    public FileUploader(String uploadServlet) {
        this.uploadServlet = uploadServlet;
    }

    /**
     * 上传文件
     *
     * @param fileName        文件名
     * @param fileContentType 文件content type
     * @param file            要上传的文件
     * @param requestParams   请求参数
     * @return 上传后的返回响应
     */
    public String upload(String fileName, String fileContentType, File file, Map<String, String> requestParams) {
        String resp = "";

        //使用Local文件系统时
        if(FileUploaderUtil.isLocal()){
            Part[] parts = null;
            final HttpClient client = HttpClientFactory.getInstance().createHttpClient();
            final PostMethod filePost = new PostMethod(uploadServlet);
            try {

                if (requestParams != null && !requestParams.isEmpty()) {
                    parts = new Part[requestParams.size() + 1];
                    int index = 0;
                    for (Map.Entry<String, String> entry : requestParams.entrySet()) {

                      if(entry.getKey().equalsIgnoreCase(PRI_PATH_REQUEST_PARAM_KEY)){
                            parts[index] = new StringPart(entry.getKey(), entry.getValue().replaceAll("\\.","_"));
                        }else {
                            parts[index] = new StringPart(entry.getKey(), entry.getValue());
                        }
                        index++;
                    }
                }
                parts[parts.length - 1] = new FilePart(fileName, fileName, file, fileContentType, "UTF-8");
                filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

                client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT_FIRST);
                int status = client.executeMethod(filePost);
                LOG.info("http client execute status : " + status);
                if (status == HttpStatus.SC_OK) {
                    LOG.debug("上传成功");
                    resp = filePost.getResponseBodyAsString();

                } else {
                    LOG.warn("上传失败,重新试一次。。。");
                    client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT_SECOND);
                    status = client.executeMethod(filePost);
                    if (status == HttpStatus.SC_OK)
                        resp = filePost.getResponseBodyAsString();


                }
            } catch (Exception e) {
                e.printStackTrace();
                //TODO
            } finally {
                filePost.releaseConnection();
            }
        }else {
            //使用local文件系统以外时
            short fileType = 0;
            if(uploadServlet.endsWith(FILE_ACTION_URL)){
                fileType = 0;
            }else if(uploadServlet.endsWith(IMAGE_ACTION_URL)){
                fileType = 1;
            }else if(uploadServlet.endsWith(ICON_ACTION_URL)){
                fileType = 2;
            }
            resp = FileUploaderUtil.upload((short)fileType,fileName, file, requestParams);     //这里上传到s3
        }

        LOG.debug(resp);
        return resp;
    }

fileuploader.properties是s3服务的配置文件:

# s3,local,fastdfs
file_system=s3

# Access Credentials
access_key_id=
secret_access_key=
bucket_name=dev
end_point=https://s3.cn-north-1.amazonaws.com.cn

public class FileUploaderUtil {

......


 // Initialize S3 client configurations
    static {
        try {

            properties = new Properties();
            properties.load(FileUploaderUtil.class.getResourceAsStream(FILEUPLOADER_PROPERTIES));
            URL url = FileUploaderUtil.class.getResource("/");//当前的classpath的绝对URI路径
            uploadTemPath = new StringBuilder(url.getPath()).append(File.separator).append(UPLOAD_TEMP_DIR).toString();

            //本地配置文件中的配置项
            fileSystem = properties.getProperty(FILE_SYSTEM).trim();
            //zookeeper中如有对象配置项,以此为准
            String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, FILE_SYSTEM);
            if(StringUtils.isNotEmpty(zkFileSystem)){
                fileSystem = zkFileSystem;
            }

            if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_S3)) {

                //本地配置文件中的配置项
                awsAccessKey = properties.getProperty(AWS_ACCESS_KEY).trim();
                //zookeeper中如有对象配置项,以此为准
                String zkAwsAccessKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_ACCESS_KEY);
                if(StringUtils.isNotEmpty(zkAwsAccessKey)){
                    awsAccessKey = zkAwsAccessKey;
                }

                //本地配置文件中的配置项
                awsSecretKey = properties.getProperty(AWS_SECRET_KEY).trim();
                //zookeeper中如有对象配置项,以此为准
                String zkAwsSecretKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_SECRET_KEY);
                if(StringUtils.isNotEmpty(zkAwsSecretKey)){
                    awsSecretKey = zkAwsSecretKey;
                }

                //本地配置文件中的配置项
                bucketName = properties.getProperty(BUCKET_NAME).trim();
                //zookeeper中如有对象配置项,以此为准
                String zkBucketName = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, BUCKET_NAME);
                if(StringUtils.isNotEmpty(zkBucketName)){
                    bucketName = zkBucketName;
                }

                //本地配置文件中的配置项
                endPoint = properties.getProperty(END_POINT).trim();
                //zookeeper中如有对象配置项,以此为准
                String zkEndPoint = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, END_POINT);
                if(StringUtils.isNotEmpty(zkEndPoint)){
                    endPoint = zkEndPoint;
                }

                BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
                s3Client = new AmazonS3Client(awsCreds);
                s3Client.setEndpoint(endPoint);

                LOG.info("[FileManager]S3 connection established.");
            }

            //上传临时目录
            File uploadTemp = new File(uploadTemPath);
            // 创建目录
            if (!uploadTemp.exists()) {
                uploadTemp.mkdirs();// 目录不存在的情况下,创建目录。
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            LOG.error(ex.getMessage(), ex);
        }
    }

    public static Boolean isS3(){
        //zookeeper中如有对象配置项,以此为准
        String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, FILE_SYSTEM);
        if(StringUtils.isNotEmpty(zkFileSystem)){
            fileSystem = zkFileSystem;
        }

        if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_S3)) {
            return  true;
        }else {
            return  false;
        }

    }

    public static Boolean isLocal(){
        //zookeeper中如有对象配置项,以此为准
        String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, FILE_SYSTEM);
        if(StringUtils.isNotEmpty(zkFileSystem)){
            fileSystem = zkFileSystem;
        }

        if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_LOCAL)) {
            return  true;
        }else {
            return  false;
        }

    }

    public static Boolean isFastDFS(){
        //zookeeper中如有对象配置项,以此为准
        String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, FILE_SYSTEM);
        if(StringUtils.isNotEmpty(zkFileSystem)){
            fileSystem = zkFileSystem;
        }

        if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_FASTDFS)) {
            return  true;
        }else {
            return  false;
        }

    }

    public static String[] getS3GroupAndName(String fileUrl){

        //本地配置文件中的配置项
        endPoint = properties.getProperty(END_POINT).trim();
        //zookeeper中如有对象配置项,以此为准
        String zkEndPoint = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, END_POINT);
        if(StringUtils.isNotEmpty(zkEndPoint)){
            endPoint = zkEndPoint;
        }

        String[] strArr = endPoint.split("https://");//end_point=https://s3.cn-north-1.amazonaws.com.cn
        StringBuilder s3UrlSplit = new StringBuilder(".").append(strArr[1]).append("/"); //.s3.cn-north-1.amazonaws.com.cn/

        //"https://rsbucket.s3.cn-north-1.amazonaws.com.cn/2015/05/29/60b9406e-8cf6-4bba-b0fc-xxxxxxxxxx.jpg"
        //rsbucket,2015/05/29/60b9406e-8cf6-4bba-b0fc-xxxxxxxxxxx.jpg
        String remoteName = StringUtils.substringAfter(fileUrl, s3UrlSplit.toString());

        //设定group为bucketName,不从URL中截取
        //处理file_system多次切换时,bucketName为Null,从zookeeper中获取配置
        String remoteGroup = (bucketName!=null)?bucketName:SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, BUCKET_NAME); //StringUtils.substringAfter(StringUtils.substringBefore(fileUrl, s3UrlSplit.toString()), "://");

        String[] rlt = new String[]{remoteGroup,remoteName};

        return  rlt;
    }

    /**
     * 上传文件
     * IMAGE、ICON图片切割处理,适配原FileUploadServlet、ImageUploadServlet、IconUploadServlet
     * @param fileName      文件名
     * @param file          要上传的文件
     * @param requestParams 请求参数
     * @return 上传后的返回响应
     */
    public static String upload(short fileType, String fileName, File file, Map<String, String> requestParams) {

        //每次上传时获取最新的bucketName
        //zookeeper中如有对象配置项,以此为准
        String zkBucketName = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, BUCKET_NAME);
        if(StringUtils.isNotEmpty(zkBucketName)){
            bucketName = zkBucketName;
        }
        //从local切换为s3,未创建s3Client时
        if (s3Client == null) {

            //本地配置文件中的配置项
            awsAccessKey = properties.getProperty(AWS_ACCESS_KEY).trim();
            //zookeeper中如有对象配置项,以此为准
            String zkAwsAccessKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_ACCESS_KEY);
            if(StringUtils.isNotEmpty(zkAwsAccessKey)){
                awsAccessKey = zkAwsAccessKey;
            }

            //本地配置文件中的配置项
            awsSecretKey = properties.getProperty(AWS_SECRET_KEY).trim();
            //zookeeper中如有对象配置项,以此为准
            String zkAwsSecretKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_SECRET_KEY);
            if(StringUtils.isNotEmpty(zkAwsSecretKey)){
                awsSecretKey = zkAwsSecretKey;
            }

            //本地配置文件中的配置项
            endPoint = properties.getProperty(END_POINT).trim();
            //zookeeper中如有对象配置项,以此为准
            String zkEndPoint = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, END_POINT);
            if(StringUtils.isNotEmpty(zkEndPoint)){
                endPoint = zkEndPoint;
            }

            BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
            s3Client = new AmazonS3Client(awsCreds);
            s3Client.setEndpoint(endPoint);

            LOG.info("[FileManager]S3 connection established.");
        }

        String resp = "";

        //Request请求参数
        String imgCutFlg = HTTP_POST_PARAM_IMAGE_CUT;//http的参数,用来标注上传的图片是否除了原图之外还生成其他的剪裁后的图片 1:剪裁, -1:不剪裁
        boolean iconCutFlg = false;

        int xCop = 0;
        int yCop = 0;
        int xWidth = 0;
        int yHeight = 0;

        for (Map.Entry<String, String> entry : requestParams.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if (PRI_PATH_REQUEST_PARAM_KEY.equalsIgnoreCase(key)) {
                if (StringUtils.isNotEmpty(value)) {
                    priPath = value.replaceAll("\\.", "_");
                }
            }

            if (HTTP_POST_PARAM_TYPE.equalsIgnoreCase(key)) {
                if (StringUtils.isNotEmpty(value)) {
                    if (value.equals(HTTP_POST_PARAM_IMAGE_NOTCUT)) {
                        imgCutFlg = HTTP_POST_PARAM_IMAGE_NOTCUT;
                    }
                }
            }

            if (ICON_COORDINATE_TAG.equalsIgnoreCase(key)) {
                iconCutFlg = true;
                if (StringUtils.isNotEmpty(value)) {
                    JSONObject jsonObj = JSONObject.fromObject(value);
                    xCop = jsonObj.getInt(ICON_X_COORIDNATE);
                    yCop = jsonObj.getInt(ICON_Y_COORIDNATE);
                    xWidth = jsonObj.getInt(ICON_X_WIDTH);
                    yHeight = jsonObj.getInt(ICON_Y_HEIGHT);
                }
            }
        }

        //获取扩展名
        String ext = StringUtils.EMPTY;
        if (StringUtils.INDEX_NOT_FOUND != StringUtils.indexOf(fileName, ".")) {
            ext = StringUtils.substring(fileName,
                    StringUtils.lastIndexOf(fileName, ".") + 1);
            ext = StringUtils.trimToEmpty(ext);
        }

        //文件接收到临时随机目录中
        Calendar c = Calendar.getInstance();
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        String tmpDir = (f.format(c.getTime())) + "_" + UUID.randomUUID().toString();
        String fullpathTmpDirStr = uploadTemPath+ File.separator + tmpDir;
        // 创建临时目录
        File fullpathTmpDir = new File(fullpathTmpDirStr);
        if (!fullpathTmpDir.exists()) {
            fullpathTmpDir.mkdirs();// 临时目录不存在时创建目录。
        }

        //接收上传文件
        File receivedFile = receiveFile(fileName, file, fullpathTmpDirStr);

        //上传S3
        String url = uploadInternal(fileName, receivedFile, null, priPath);

        JSONArray rltJsonArray = new JSONArray();

        //IMAGE、ICON图片切割处理
        if (UPLOAD_TYPE_FILE == fileType) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", fileName);
            jsonObject.put("url", url);
            jsonObject.put("suffix", ext);
            rltJsonArray.add(jsonObject);

            resp = rltJsonArray.toString();

        } else if (UPLOAD_TYPE_IMAGE == fileType) {
            JSONObject o = new JSONObject();
            o.put("name", fileName);
            o.put("url", url);
            o.put("suffix", ext);

            String smallImgKey = getImgFileKey(url, SMALL_PREFIX);
            String largeImgKey = getImgFileKey(url, LARGE_PREFIX);

            if (imgCutFlg.equalsIgnoreCase(HTTP_POST_PARAM_IMAGE_CUT)) {
                ImageConvertModel sImageConvertModel = cutAndUploadSmallImg(fileName, receivedFile, ext, fullpathTmpDirStr, smallImgKey,priPath);
                ImageConvertModel lImageConvertModel = cutAndUploadLargeImg(fileName, receivedFile, ext, fullpathTmpDirStr, largeImgKey,priPath);

                o.put("s_url", sImageConvertModel.getUrl());
                o.put("l_url", lImageConvertModel.getUrl());

                ImageInfo sinfo = sImageConvertModel.getImageInfo();
                ImageInfo linfo = lImageConvertModel.getImageInfo();
                if (sinfo != null) {
                    JSONObject sizeJson = new JSONObject();
                    JSONObject smallJson = new JSONObject();
                    JSONObject normalJson = new JSONObject();
                    smallJson.put("width", sinfo.getWidth());
                    smallJson.put("height", sinfo.getHeight());
                    normalJson.put("width", sinfo.getSrcWidth());
                    normalJson.put("height", sinfo.getSrcHeight());
                    sizeJson.put("small", smallJson);
                    sizeJson.put("origin", normalJson);
                    if (linfo != null) {
                        JSONObject largeJson = new JSONObject();
                        largeJson.put("width", linfo.getWidth());
                        largeJson.put("height", linfo.getHeight());
                        sizeJson.put("large", largeJson);
                    }
                    o.put("size", sizeJson);
                }
            }

            rltJsonArray.add(o);

            resp = rltJsonArray.toString();

        } else if (UPLOAD_TYPE_ICON == fileType) {
            String smallIconUrl = "";
            String largeIconUrl = "";

            JSONObject o = new JSONObject();
            o.put("name", fileName);
            o.put("url", url);
            o.put("suffix", ext);

            String smallImgKey = getImgFileKey(url, SMALL_PREFIX);
            String largeImgKey = getImgFileKey(url, LARGE_PREFIX);

            if (iconCutFlg) {
                String rlt = cutAndUploadIcon(xCop, yCop, xWidth, yHeight, fileName, receivedFile, ext, fullpathTmpDirStr, smallImgKey, largeImgKey,priPath);
                String[] urlArray = rlt.split(SPLIT_COMMA);
                if (urlArray.length == 2) {
                    o.put("s_url", urlArray[0]);
                    o.put("l_url", urlArray[1]);
                }
            } else {
                smallIconUrl = cutAndUploadSmallIcon(fileName, receivedFile, ext, fullpathTmpDirStr, smallImgKey, priPath);
                largeIconUrl = cutAndUploadLargeIcon(fileName, receivedFile, ext, fullpathTmpDirStr, largeImgKey, priPath);

                o.put("s_url", smallIconUrl);
                o.put("l_url", largeIconUrl);
            }

            rltJsonArray.add(o);

            resp = rltJsonArray.toString();
        }

        //删除接收文件临时目录
        FileDeleteStrategy.FORCE.deleteQuietly(fullpathTmpDir);

        return resp;
    }

    /**
     * 根据原图url生成小图、中图的FileKey
     * @param url
     * @param prefix
     * @return
     */
    private static String getImgFileKey(String url, String prefix) {
        String key = "";
        String[] strArr = endPoint.split("https://");//end_point=https://s3.cn-north-1.amazonaws.com.cn
        StringBuilder s3UrlSplit = new StringBuilder(".").append(strArr[1]).append("/"); //.s3.cn-north-1.amazonaws.com.cn/
        if (StringUtils.contains(url, s3UrlSplit.toString())) {
            String fileName = StringUtils.substringAfter(url, s3UrlSplit.toString());
            if (prefix.equalsIgnoreCase(SMALL_PREFIX) || prefix.equalsIgnoreCase(LARGE_PREFIX)) {

                String fileName_1 = StringUtils.substringBeforeLast(fileName, "/");
                String fileName_2 = StringUtils.substringAfterLast(fileName, "/");

                StringBuilder sb = new StringBuilder();
                sb.append(fileName_1);
                sb.append("/");
                sb.append(prefix);
                sb.append(fileName_2);

                key = sb.toString();
            }

        }
        return key;
    }

    private static File receiveFile(String fileName, File file,String receiveTempDirPath) {
        int BUFFER_SIZE = 16 * 1024;
        File receivedFile = null;

        //通过fileupload上传的临时文件,接收并上传
        receivedFile = new File(receiveTempDirPath, fileName);

        InputStream in = null;
        OutputStream out = null;
        try {

            in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
            out = new BufferedOutputStream(new FileOutputStream(receivedFile), BUFFER_SIZE);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

        return receivedFile;
    }

    /**
     * 上传文件处理接口
     * 根据不同文件系统调用相应上传方法
     * 当前只开启S3
     * @param fileName
     * @param file
     * @param fileKeyName
     * @param priPath
     * @return
     */
    private static String uploadInternal(String fileName, File file, String fileKeyName, String priPath) {

        String fileUrl = "";

        //S3
        if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_S3)) {
            fileUrl = uploadInternalS3(fileName, file, fileKeyName, priPath);
        }

        return fileUrl;
    }

    /**
     * S3文件系统上传
     * @param fileName
     * @param file
     * @param fileKeyName
     * @return
     */
    private static String uploadInternalS3(String fileName, File file, String fileKeyName, String priPath) {
        String key = "";

        if (StringUtil.isEmpty(fileKeyName)) {

            //获取扩展名
            String ext = StringUtils.EMPTY;
            if (StringUtils.INDEX_NOT_FOUND != StringUtils.indexOf(fileName, ".")) {
                ext = StringUtils.substring(fileName,
                        StringUtils.lastIndexOf(fileName, ".") + 1);
                ext = StringUtils.trimToEmpty(ext);
            }

            //用随机生成的文件名作为Key
            Calendar c = Calendar.getInstance();
            SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");

            //将tenantId作为FileKey的前缀
            key = priPath + "/" + (f.format(c.getTime())) + "/" + UUID.randomUUID().toString() + "." + ext;
        } else {
            //priPath/yyyy/MM/dd
            if(fileKeyName.startsWith(priPath)){
                Calendar now = Calendar.getInstance();

                //priPath与yyyy相同时
                if(priPath.equals(String.valueOf(now.get(Calendar.YEAR))) && !fileKeyName.startsWith(priPath + "/" + priPath)){
                    key = priPath + "/" + fileKeyName;
                }else{
                    key = fileKeyName;
                }

            }else{
                key = priPath + "/" + fileKeyName;
            }
        }

        PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, file).withCannedAcl(CannedAccessControlList.PublicRead);

        // Request server-side encryption.
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        putRequest.setMetadata(objectMetadata);

        //上传文件
        PutObjectResult response = s3Client.putObject(putRequest);

        LOG.info("[FileManager]Uploaded object encryption status is: {}",response.getSSEAlgorithm());

        //生成公用的url
        GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                bucketName, key);
        URL url = s3Client.generatePresignedUrl(urlRequest);

        String fileUrl = "";

        //去除URL预签名信息
        fileUrl = url.toString();
        String[] urlStrArray = fileUrl.split("\\?");

        fileUrl = (urlStrArray.length >= 1) ? urlStrArray[0] : url.toString();

        Object[] placeholder = {file.getName(), fileUrl};
        LOG.info("[FileManager]Uploaded {} to {}", placeholder);

        return fileUrl;
    }



.........
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值