1. 程式人生 > >上傳檔案到七牛雲並刪除已上傳檔案

上傳檔案到七牛雲並刪除已上傳檔案

@Configuration
@ConfigurationProperties(prefix = "qiniu")
public class QiNiuConfig {

    private String accessKey;

    private String secretKey;

    /**  要上傳的空間名 */
    private String bucket;

    /** 對應儲存空間的訪問域名 */
    private String path;

    /** 需要上傳的檔案所在的目錄*/
    private String filePath;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public String getBucket() {
        return bucket;
    }

    public String getPath() {
        return path;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public void setPath(String path) {
        this.path = path;
    }
}



/**
 * 
 * @param filePath 檔案路徑
 * @param qiNiuConfig 七牛雲上傳資訊配置
 * @return
 */
public class QiNiuUtil {
    private static final Logger logger = LoggerFactory.getLogger(QiNiuUtil.class);

    public static Map<String, String> qiniuUpload(String filePath, QiNiuConfig qiNiuConfig) {
        //上傳到七牛後儲存的檔名
        String key;
        //金鑰配置
        Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
        //自動識別要上傳的空間的初村區域是華東、華北、華南
        Zone z = Zone.autoZone();
        com.qiniu.storage.Configuration c = new com.qiniu.storage.Configuration(z);
        //建立上傳物件
        UploadManager uploadManager = new UploadManager(c);
        //獲取upToken
        String upToken = auth.uploadToken(qiNiuConfig.getBucket());
        //開始時間
        long startTime = System.currentTimeMillis();
        String timeString = String.valueOf(startTime);
        Map<String, String> map = new HashMap<String, String>();
        File file = new File(filePath);
        if (file.exists()) {
            try {
                key = filePath.substring(filePath.lastIndexOf("/") + 1);
                Response response = uploadManager.put(filePath, key, upToken);
                map.put("state", "1");
                map.put("info", "上傳七牛成功");
                map.put("fileName", key);
                map.put("qiniuUrl", qiNiuConfig.getPath() + "/" + key);
                logger.info("上傳成功", response.body());
            } catch (QiniuException e) {
                map.put("state", "0");
                map.put("info", "上傳七牛失敗");
                logger.error("上傳七牛異常={}");
                logger.error(e.getMessage(), e);
            } finally {
                //上傳七牛完成後刪除本地檔案
                File deleteFile = new File(filePath);
                logger.info("刪除: " + filePath);
                if (deleteFile.exists()) {
                    deleteFile.delete();
                }
            }
        }
        //結束時間
        long endTime = System.currentTimeMillis();
        logger.info("______________上傳用時:={}", endTime - startTime + "ms");
        return map;
    }
}