com.qiniu.util.StringMap Java Examples

The following examples show how to use com.qiniu.util.StringMap. 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: UserController.java    From MyCommunity with Apache License 2.0 6 votes vote down vote up
@RequestMapping(path = "/setting", method = RequestMethod.GET)
public String getSettingPage(Model model) {
    // 上传文件名称
    String fileName = GetGenerateUUID.generateUUID();
    // 设置响应信息
    StringMap policy = new StringMap();
    policy.put("returnBody", JSONUtil.getJSONString(0));
    // 生成上传凭证
    Auth auth = Auth.create(accessKey, secretKey);
    String uploadToken = auth.uploadToken(headerBucketName, fileName, 3600, policy);

    model.addAttribute("uploadToken", uploadToken);
    model.addAttribute("fileName", fileName);

    return "/site/setting";
}
 
Example #2
Source File: QiniuFileProvider.java    From xiaoyaoji with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 上传
 *
 * @param path  文件路径
 * @param bytes 文件流
 * @return 上传后的文件夹名称
 */
@Override
public String upload(String path, byte[] bytes) throws IOException {
    Auth auth = Auth.create(ConfigUtils.getQiniuAccessKey(), ConfigUtils.getQiniuSecretKey());
    UploadManager uploadManager = new UploadManager();
    try {
        String token = auth.uploadToken(ConfigUtils.getBucketURL(), path, 3600, new StringMap().put("insertOnly", 0));
        Response res = uploadManager.put(bytes, path, token);
        String result = res.bodyString();
        logger.debug("qiniuUpload:" + result);
        if (result.contains("\"key\"")) {
            return path;
        }
        throw new IOException(result);
    } catch (QiniuException e) {
        Response r = e.response;
        throw new IOException(r.bodyString());
    }
}
 
Example #3
Source File: QiniuProvider.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> createUploadToken(UploadTokenParam param) {
	
	if(StringUtils.isNotBlank(param.getCallbackUrl())){
		if(StringUtils.isBlank(param.getCallbackBody())){
			param.setCallbackBody(DEFAULT_CALLBACK_BODY);
		}
		if(StringUtils.isBlank(param.getCallbackHost())){
			param.setCallbackHost(host);
		}
	}
	
	Map<String, Object> result = new HashMap<>();
	StringMap policy = new StringMap();
	policy.putNotNull(policyFields[0], param.getCallbackUrl());
	policy.putNotNull(policyFields[1], param.getCallbackBody());
	policy.putNotNull(policyFields[2], param.getCallbackHost());
	policy.putNotNull(policyFields[3], param.getCallbackBodyType());
	policy.putNotNull(policyFields[4], param.getFileType());
	policy.putNotNull(policyFields[5], param.getFileKey());
	policy.putNotNull(policyFields[6], param.getMimeLimit());
	policy.putNotNull(policyFields[7], param.getFsizeMin());
	policy.putNotNull(policyFields[8], param.getFsizeMax());
	policy.putNotNull(policyFields[9], param.getDeleteAfterDays());

	String token = auth.uploadToken(bucketName, param.getFileKey(), param.getExpires(), policy, true);
	result.put("uptoken", token);
	result.put("host", this.urlprefix);
	result.put("dir", param.getUploadDir());
	
	return result;
}
 
Example #4
Source File: QiniuUtil.java    From BigDataPlatform with GNU General Public License v3.0 4 votes vote down vote up
public static String getUpToken() {
    return auth.uploadToken(bucket, null, 3600, new StringMap().put("insertOnly", 1));
}
 
Example #5
Source File: QiniuOssServiceImpl.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	this.putPolicy = new StringMap();
	putPolicy.put("returnBody",
			"{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
}
 
Example #6
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 4 votes vote down vote up
@Override
public void afterPropertiesSet() {
	this.putPolicy = new StringMap();
	putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
}
 
Example #7
Source File: QiNiuCloudUtil.java    From SpringBoot-Home with Apache License 2.0 4 votes vote down vote up
public static String getUpToken() {
    //生成凭证
    return auth.uploadToken(bucketName, null, 3600, new StringMap().put("insertOnly", 1));
}
 
Example #8
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 4 votes vote down vote up
@Override
public void afterPropertiesSet() {
	this.putPolicy = new StringMap();
	putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
}
 
Example #9
Source File: QiniuOssFileHandler.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public UploadResult upload(MultipartFile file) {
    Assert.notNull(file, "Multipart file must not be null");

    Region region = optionService.getQiniuRegion();

    String accessKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_ACCESS_KEY).toString();
    String secretKey = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_SECRET_KEY).toString();
    String bucket = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_BUCKET).toString();
    String protocol = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_PROTOCOL).toString();
    String domain = optionService.getByPropertyOfNonNull(QiniuOssProperties.OSS_DOMAIN).toString();
    String source = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_SOURCE, String.class, "");
    String styleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_STYLE_RULE, String.class, "");
    String thumbnailStyleRule = optionService.getByPropertyOrDefault(QiniuOssProperties.OSS_THUMBNAIL_STYLE_RULE, String.class, "");

    // Create configuration
    Configuration configuration = new Configuration(region);

    // Create auth
    Auth auth = Auth.create(accessKey, secretKey);
    // Build put plicy
    StringMap putPolicy = new StringMap();
    putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"size\":$(fsize),\"width\":$(imageInfo.width),\"height\":$(imageInfo.height)}");
    // Get upload token
    String uploadToken = auth.uploadToken(bucket, null, 60 * 60, putPolicy);

    // Create temp path
    Path tmpPath = Paths.get(System.getProperty("java.io.tmpdir"), bucket);

    StringBuilder basePath = new StringBuilder(protocol)
        .append(domain)
        .append(URL_SEPARATOR);

    try {
        String basename = FilenameUtils.getBasename(Objects.requireNonNull(file.getOriginalFilename()));
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String timestamp = String.valueOf(System.currentTimeMillis());
        StringBuilder upFilePath = new StringBuilder();
        if (StringUtils.isNotEmpty(source)) {
            upFilePath.append(source)
                .append(URL_SEPARATOR);
        }
        upFilePath.append(basename)
            .append("_")
            .append(timestamp)
            .append(".")
            .append(extension);

        // Get file recorder for temp directory
        FileRecorder fileRecorder = new FileRecorder(tmpPath.toFile());
        // Get upload manager
        UploadManager uploadManager = new UploadManager(configuration, fileRecorder);
        // Put the file
        Response response = uploadManager.put(file.getInputStream(), upFilePath.toString(), uploadToken, null, null);

        if (log.isDebugEnabled()) {
            log.debug("Qiniu oss response: [{}]", response.toString());
            log.debug("Qiniu oss response body: [{}]", response.bodyString());
        }

        // Convert response
        PutSet putSet = JsonUtils.jsonToObject(response.bodyString(), PutSet.class);

        // Get file full path
        String filePath = StringUtils.join(basePath.toString(), upFilePath.toString());

        // Build upload result
        UploadResult result = new UploadResult();
        result.setFilename(basename);
        result.setFilePath(StringUtils.isBlank(styleRule) ? filePath : filePath + styleRule);
        result.setKey(upFilePath.toString());
        result.setSuffix(extension);
        result.setWidth(putSet.getWidth());
        result.setHeight(putSet.getHeight());
        result.setMediaType(MediaType.valueOf(Objects.requireNonNull(file.getContentType())));
        result.setSize(file.getSize());

        if (isImageType(result.getMediaType())) {
            if (ImageUtils.EXTENSION_ICO.equals(extension)) {
                result.setThumbPath(filePath);
            } else {
                result.setThumbPath(StringUtils.isBlank(thumbnailStyleRule) ? filePath : filePath + thumbnailStyleRule);
            }
        }

        return result;
    } catch (IOException e) {
        if (e instanceof QiniuException) {
            log.error("Qiniu oss error response: [{}]", ((QiniuException) e).response);
        }

        throw new FileOperationException("上传附件 " + file.getOriginalFilename() + " 到七牛云失败", e);
    }
}
 
Example #10
Source File: QiniuCloud.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param fileKey
 * @return
 * @see #formatFileKey(String)
 */
public String getUploadToken(String fileKey) {
	// 上传策略参见 https://developer.qiniu.com/kodo/manual/1206/put-policy
	StringMap policy = new StringMap().put("fsizeLimit", 102400000);  // 100M
	return auth.uploadToken(bucketName, fileKey, 60 * 10, policy);
}
 
Example #11
Source File: QiNiuServiceImpl.java    From spring-boot-demo with MIT License 4 votes vote down vote up
@Override
public void afterPropertiesSet() {
	this.putPolicy = new StringMap();
	putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
}
 
Example #12
Source File: QiniuServiceImpl.java    From jframe with Apache License 2.0 4 votes vote down vote up
@Override
public String uploadToken(String id, String key, long expires, StringMap policy) {
    return uploadToken(id, key, expires, policy, true);
}
 
Example #13
Source File: QiniuServiceImpl.java    From jframe with Apache License 2.0 4 votes vote down vote up
@Override
public String uploadToken(String id, String key, long expires, StringMap policy, boolean strict) {
    String bucket = CONFIG.getConf(id, QiniuConfig.BUCKET);
    if (expires == -1) expires = Long.parseLong(CONFIG.getConf(id, QiniuConfig.UPLOAD_EXPIRE, "3600"));
    return AUTH.uploadToken(bucket, key, expires, policy, strict);
}
 
Example #14
Source File: QiniuService.java    From jframe with Apache License 2.0 votes vote down vote up
String uploadToken(String id, String key, long expires, StringMap policy); 
Example #15
Source File: QiniuService.java    From jframe with Apache License 2.0 votes vote down vote up
String uploadToken(String id, String key, long expires, StringMap policy, boolean strict);