Java Code Examples for com.qiniu.http.Response#needRetry()

The following examples show how to use com.qiniu.http.Response#needRetry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QiNiuCloudTemplate.java    From magic-starter with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SneakyThrows
private OssFile upload(String bucketName, String fileName, InputStream stream, boolean cover) {
	// 创建存储桶
	createBucket(bucketName);
	// 获取 oss 存储文件名
	String key = getFileName(fileName);

	// 是否覆盖上传
	if (cover) {
		uploadManager.put(stream, key, getUploadToken(bucketName, key), null, null);
	} else {
		Response response = uploadManager.put(stream, key, getUploadToken(bucketName), null, null);
		int retry = 0;
		int retryCount = 5;
		while (response.needRetry() && retry < retryCount) {
			response = uploadManager.put(stream, key, getUploadToken(bucketName), null, null);
			retry++;
		}
	}

	OssFile ossFile = new OssFile();
	ossFile.setName(key);
	ossFile.setOriginalName(fileName);
	ossFile.setLink(getFileLink(bucketName, key));
	return ossFile;
}
 
Example 2
Source File: FileService.java    From ml-blog with MIT License 6 votes vote down vote up
/**
 * 文件删除
 * @param key
 * @return
 */
public Response delete(String key) throws GlobalException{

    // 有配置数据,但上传凭证为空
    if (!commonMap.containsKey("upToken")) {
        // 创建七牛云组件
        createQiniuComponent();
    }

    try {
        BucketManager bucketManager = (BucketManager) commonMap.get("bucketManager");
        String bucket = commonMap.get("qn_bucket").toString();
        Response response = bucketManager.delete(bucket, key);
        int retry = 0;
        while(response.needRetry() && retry < 3) {
            response = bucketManager.delete(bucket, key);
            retry++;
        }
        return response;
    } catch (QiniuException ex) {
        log.error("文件删除异常:",ex.toString());
        throw new GlobalException(500, ex.toString());
    }
}
 
Example 3
Source File: QiniuTemplate.java    From blade-tool with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
	makeBucket(bucketName);
	key = getFileName(key);
	// 覆盖上传
	if (cover) {
		uploadManager.put(stream, key, getUploadToken(bucketName, key), null, null);
	} else {
		Response response = uploadManager.put(stream, key, getUploadToken(bucketName), null, null);
		int retry = 0;
		int retryCount = 5;
		while (response.needRetry() && retry < retryCount) {
			response = uploadManager.put(stream, key, getUploadToken(bucketName), null, null);
			retry++;
		}
	}
	BladeFile file = new BladeFile();
	file.setName(key);
	file.setLink(fileLink(bucketName, key));
	return file;
}
 
Example 4
Source File: QiniuOssServiceImpl.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean deleteFile(FileInfo fileInfo) {
	try {
		Response response = bucketManager.delete(this.bucket, fileInfo.getPath());
		int retry = 0;
		while (response.needRetry() && retry++ < 3) {
		    response = bucketManager.delete(bucket, fileInfo.getPath());
		}
	} catch (QiniuException e) {
		return false ;
	}
       return true;

}
 
Example 5
Source File: QiniuOSSAutoConfigure.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean deleteFile(FileInfo fileInfo) {
    try {
        Response response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath());
        int retry = 0;
        while (response.needRetry() && retry++ < 3) {
            response = bucketManager.delete(fileProperties.getOss().getBucketName(), fileInfo.getPath());
        }
    } catch (QiniuException e) {
        return false;
    }
    return true;
}
 
Example 6
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 七牛云上传文件
 *
 * @param file 文件
 * @return 七牛上传Response
 * @throws QiniuException 七牛异常
 */
@Override
public Response uploadFile(File file) throws QiniuException {
	Response response = this.uploadManager.put(file, file.getName(), getUploadToken());
	int retry = 0;
	while (response.needRetry() && retry < 3) {
		response = this.uploadManager.put(file, file.getName(), getUploadToken());
		retry++;
	}
	return response;
}
 
Example 7
Source File: FileService.java    From ml-blog with MIT License 5 votes vote down vote up
/**
 * 文件上传
 * @param inputStream
 * @param filename
 * @return
 */
public Response upload(InputStream inputStream, String filename) throws GlobalException{

    // 有配置数据,但上传凭证为空
    if (!commonMap.containsKey("upToken")) {
        // 创建七牛云组件
        createQiniuComponent();
    }

    try {

        String upToken = (String) commonMap.get("upToken");
        UploadManager uploadManager = (UploadManager) commonMap.get("uploadManager");
        // 上传
        Response response = uploadManager.put(inputStream,filename,upToken,null, null);
        int retry = 0;
        while(response.needRetry() && retry < 3) {
            response = uploadManager.put(inputStream,filename,upToken,null, null);
            retry++;
        }

        return response;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.error("文件上传异常:",r.toString());
        throw new GlobalException(500, ex.toString());
    }
}
 
Example 8
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 七牛云上传文件
 *
 * @param file 文件
 * @return 七牛上传Response
 * @throws QiniuException 七牛异常
 */
@Override
public Response uploadFile(File file) throws QiniuException {
	Response response = this.uploadManager.put(file, file.getName(), getUploadToken());
	int retry = 0;
	while (response.needRetry() && retry < 3) {
		response = this.uploadManager.put(file, file.getName(), getUploadToken());
		retry++;
	}
	return response;
}
 
Example 9
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 七牛云上传文件
 *
 * @param file 文件
 * @return 七牛上传Response
 * @throws QiniuException 七牛异常
 */
@Override
public Response uploadFile(File file) throws QiniuException {
	Response response = this.uploadManager.put(file, file.getName(), getUploadToken());
	int retry = 0;
	while (response.needRetry() && retry < 3) {
		response = this.uploadManager.put(file, file.getName(), getUploadToken());
		retry++;
	}
	return response;
}