当前位置:   article > 正文

文件上传示例(上传到amazon s3服务器)_jsonarray array = jsonarray.fromobject

jsonarray array = jsonarray.fromobject

文件上传示例(上传到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;
                }
            }
            ......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

工具类上传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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

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;
    }



.........
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/285787
推荐阅读
相关标签
  

闽ICP备14008679号