1. ホーム
  2. ジャワ

ファイルアップロードの例(amazon s3サーバーへ)

2022-02-25 05:58:24
<パス

ファイルアップロードの例(amazon s3サーバーにアップロードする場合)

アクションコールアップロードツールクラス

@Action(value = "files-upload",
            results = {@Result(name = SUCCESS, location = COMMON_PAGES_ROOT + "/noData.jsp")})
    public String filesUpload() {
        try {
// temporary files generated by the interceptor
            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 to server - "upload to 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()); //query condition parameter

        String uploadResponse = new FileUploader(sb.toString()).upload(uploadFileName, uploadContentType, uploadFile, params);
          /** The FileUploader tool class uploads a file to
          s3 file [{"name":"Address Book Pinyin Processing.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";
    /**
     * The servlet url for the upload
     */
    private final String uploadServlet;

    public String getUploadServlet() {
        return uploadServlet;
    }

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

    /**
     * Upload a file
     *
     * @param fileName file name
     * @param fileContentType file content type
     * @param file The file to be uploaded
     * @param requestParams request parameters
     * @return Return response after upload
     */
    public String upload(String fileName, String fileContentType, File file, Map<String, String> requestParams) {
        String resp = "";

        //When using Local file system
        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("Upload successful");
                    resp = filePost.getResponseBodyAsString();

                } else {
                    LOG.warn("Upload failed, try again. ");
                    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 {
            //when using other than the local file system
            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); //upload to s3 here
        }

        LOG.debug(resp);
        return resp;
    }


fileuploader.propertiesは、s3サービスの設定ファイルです。

# s3,ローカル,fastdfs
ファイルシステム=s3

# アクセス認証情報
access_key_id=
secret_access_key=
バケット名=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("/");//the absolute URI path of the current classpath
            uploadTemPath = new StringBuilder(url.getPath()).append(File.separator).append(UPLOAD_TEMP_DIR).toString();

            // configuration items in the local configuration file
            fileSystem = properties.getProperty(FILE_SYSTEM).trim();
            //if there are any object configuration items in zookeeper, this will prevail
            String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, FILE_SYSTEM);
            if(StringUtils.isNotEmpty(zkFileSystem)){
                fileSystem = zkFileSystem;
            }

            if (fileSystem.equalsIgnoreCase(FILE_SYSTEM_S3)) {

                // configuration items in the local configuration file
                awsAccessKey = properties.getProperty(AWS_ACCESS_KEY).trim();
                //If there are any object configuration items in zookeeper, this is the one that prevails
                String zkAwsAccessKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_ACCESS_KEY);
                if(StringUtils.isNotEmpty(zkAwsAccessKey)){
                    awsAccessKey = zkAwsAccessKey;
                }

                // configuration item in the local configuration file
                awsSecretKey = properties.getProperty(AWS_SECRET_KEY).trim();
                //If there are any object configuration items in zookeeper, this will prevail
                String zkAwsSecretKey = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, AWS_SECRET_KEY);
                if(StringUtils.isNotEmpty(zkAwsSecretKey)){
                    awsSecretKey = zkAwsSecretKey;
                }

                // configuration item in the local configuration file
                bucketName = properties.getProperty(BUCKET_NAME).trim();
                //if there are any object configuration items in zookeeper, this will prevail
                String zkBucketName = SystemConfigHelper.getInstance().getSystemConfigValue(ZOOKEEPER_CONFIG_GROUP, BUCKET_NAME);
                if(StringUtils.isNotEmpty(zkBucketName)){
                    bucketName = zkBucketName;
                }

                // configuration items in the local configuration file
                endPoint = properties.getProperty(END_POINT).trim();
                //If there is an object configuration item in zookeeper, this is the one that prevails
                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.");
            }

            //Upload temporary directory
            File uploadTemp = new File(uploadTemPath);
            // Create the directory
            if (!uploadTemp.exists()) {
                uploadTemp.mkdirs(); // Create the directory if it does not exist.
            }

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

    public static Boolean isS3(){
        // If there is an object configuration item in zookeeper, this will prevail
        String zkFileSystem = SystemConfigHelper.getInstance().getSystemConfigV
                    StringUtils.lastIndexOf(fileName, ". ") + 1);
            ext = StringUtils.trimToEmpty(ext);
        }

        // File is received in a temporary random directory
        Calendar c = Calendar.getInstance();
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        String tmpDir = (f.format(c.getTime())) + "_" + UUID.randomUUID().toString();
        String fullpathTmpDirStr = uploadTemPath+ File.separator + tmpDir;
        // Create a temporary directory
        File fullpathTmpDir = new File(fullpathTmpDirStr);
        if (!fullpathTmpDir.exists()) {
            fullpathTmpDir.mkdirs(); // Create directory if temporary directory does not exist.
        }

        // Receive the uploaded file
        File receivedFile = receiveFile(fileName, file, fullpathTmpDirStr);

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

        JSONArray rltJsonArray = new JSONArray();

        //IMAGE, ICON image cutting process
        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