All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qiniu.storage.FormUploader Maven / Gradle / Ivy

There is a newer version: 7.17.0
Show newest version
package com.qiniu.storage;

import com.qiniu.common.QiniuException;
import com.qiniu.http.AsyncCallback;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Crc32;
import com.qiniu.util.StringMap;

import java.io.File;
import java.io.IOException;

/**
 * 该类封装了七牛提供的表单上传机制
 * 参考文档:表单上传
 */
public final class FormUploader extends BaseUploader {

    private final File file;
    private final byte[] data;
    private final String mime;
    private final boolean checkCrc;
    private final ConfigHelper configHelper;
    private StringMap params;
    private String filename;
    private Client client;

    /**
     * 构建一个表单上传字节数组的对象
     */
    public FormUploader(Client client, String upToken, String key, byte[] data, StringMap params,
                        String mime, boolean checkCrc, Configuration configuration) {
        this(client, upToken, key, data, null, params, mime, checkCrc, configuration);
    }

    /**
     * 构建一个表单上传文件的对象
     */
    public FormUploader(Client client, String upToken, String key, File file, StringMap params,
                        String mime, boolean checkCrc, Configuration configuration) {
        this(client, upToken, key, null, file, params, mime, checkCrc, configuration);
    }

    private FormUploader(Client client, String upToken, String key, byte[] data, File file, StringMap params,
                         String mime, boolean checkCrc, Configuration configuration) {
        super(client, upToken, key, configuration);

        this.client = client;
        this.file = file;
        this.data = data;
        this.params = params;
        this.mime = mime;
        this.checkCrc = checkCrc;
        this.configHelper = new ConfigHelper(configuration);
    }

    @Override
    Response uploadFlows() throws QiniuException {
        buildParams();
        String host = configHelper.upHost(upToken);
        try {
            if (data != null) {
                return client.multipartPost(configHelper.upHost(upToken), params, "file", filename, data,
                        mime, new StringMap());
            } else {
                return client.multipartPost(configHelper.upHost(upToken), params, "file", filename, file,
                        mime, new StringMap());
            }
        } catch (QiniuException e) {
            if (e.response == null || e.response.needSwitchServer()) {
                changeHost(upToken, host);
            }
            throw e;
        }
    }

    /**
     * 异步上传文件
     */
    public void asyncUpload(final UpCompletionHandler handler) throws IOException {
        buildParams();
        final String host = configHelper.upHost(upToken);
        if (data != null) {
            client.asyncMultipartPost(host, params, "file", filename,
                    data, mime, new StringMap(), new AsyncCallback() {
                        @Override
                        public void complete(Response res) {
                            if (res != null && res.needSwitchServer()) {
                                changeHost(upToken, host);
                            }
                            handler.complete(key, res);
                        }
                    });
            return;
        }
        client.asyncMultipartPost(configHelper.upHost(upToken), params, "file", filename,
                file, mime, new StringMap(), new AsyncCallback() {
                    @Override
                    public void complete(Response res) {
                        if (res != null && res.needSwitchServer()) {
                            changeHost(upToken, host);
                        }
                        handler.complete(key, res);
                    }
                });
    }

    @Override
    boolean couldReloadSource() {
        return true;
    }

    @Override
    boolean reloadSource() {
        return true;
    }

    private void changeHost(String upToken, String host) {
        try {
            configHelper.tryChangeUpHost(upToken, host);
        } catch (Exception e) {
            // ignore
            // use the old up host //
        }
    }

    private void buildParams() throws QiniuException {
        if (params == null) return;
        params.put("token", upToken);

        if (key != null) {
            params.put("key", key);
        }

        if (file != null) {
            filename = file.getName();
        } else {
            Object object = params.get("filename");
            if (object != null) {
                filename = (String) object;
                object = null;
            } else if (filename == null || filename.trim().length() == 0) {
                if (key == null) {
                    filename = "defaultFilename";
                } else {
                    filename = key;
                }
            }
        }

        if (checkCrc) {
            long crc32;
            if (file != null) {
                try {
                    crc32 = Crc32.file(file);
                } catch (IOException e) {
                    throw new QiniuException(e);
                }
            } else {
                crc32 = Crc32.bytes(data);
            }
            params.put("crc32", "" + crc32);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy