七牛java sdk文件上传图片,文件名没有后缀,putRet接收的都是哈希值。查看七牛上传模板修改后为:
private static String upQiNiuFile(String localFilePath, String fileSpaceName){
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone1());
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = QiNiuConTants.AK;
String secretKey = QiNiuConTants.SK;
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(fileSpaceName);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
LOGGER.info("上传文件成功参数localFilePath:{}和fileSpaceName:{}返回结果", localFilePath, fileSpaceName, JSON.toJSONString(putRet));
return QiNiuEnum.urlOf(fileSpaceName).getUrl() + putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
LOGGER.error("上传文件参数localFilePath:{}和fileSpaceName:{},错误信息{}", localFilePath, fileSpaceName, ex);
try {
r.bodyString();
throw new DoorException(ResultCodeEnum.PROCESSING_FAILURE.getCode(), "上传文件失败");
} catch (QiniuException ex2) {
LOGGER.error("上传文件参数localFilePath:{}和fileSpaceName:{},bodyString错误信息{}", localFilePath, fileSpaceName, ex2);
throw new DoorException(ResultCodeEnum.PROCESSING_FAILURE.getCode(), "上传文件失败" + ex2);
}
}
}
默认不指定key的情况下,以文件内容的hash值作为文件名。
至于文件名后缀设置,指定key就可以了,例如:String key = "/img/test.jpg"。