use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class GithubApiClient method uploadInputStream.
@Override
public String uploadInputStream(InputStream inputStream, String fileName) {
try (InputStream uploadIs = StreamUtil.clone(inputStream)) {
Map<String, String> hears = new HashMap<>();
hears.put("Authorization", "Bearer " + token);
hears.put("Content-Type", "text/plain");
// 读取图片字节数组
byte[] data = new byte[uploadIs.available()];
uploadIs.read(data);
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64编码过的字节数组字符串
final String baseContent = encoder.encode(data);
final JSONObject jsonObject = new JSONObject();
jsonObject.put("message", fileName);
jsonObject.put("content", baseContent);
final String toString = jsonObject.toString();
String uploadUrl = requestUrl + "repos/" + user + "/" + repository + "/contents/" + fileName;
// 查询是否存在
if (exists(fileName)) {
jsonObject.put("sha", UUID.randomUUID().toString().replace("-", ""));
}
final String s = HttpUtils.JsonPut(uploadUrl, toString, hears);
return domianUrl + user + "/" + repository + "/" + fileName;
} catch (IOException e) {
throw new ServiceException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class LocalApiClient method init.
@Override
public ApiClient init(FileProperties fileProperties) {
final LocalFileProperties localFileProperties = fileProperties.getLocal();
String localUrl = localFileProperties.getLocalUrl();
String localFilePath = localFileProperties.getLocalFilePath();
if (StringUtils.isEmpty(localUrl) || StringUtils.isEmpty(localFilePath)) {
throw new ServiceException("[" + this.storageType + "]尚未配置Nginx文件服务器,文件上传功能暂时不可用!");
}
checkDomainUrl(localUrl);
this.localFilePath = localFilePath;
return this;
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class QiniuApiClient method uploadInputStream.
/**
* 上传图片
*
* @param is 图片流
* @param fileName 图片路径
* @return 上传后的路径
*/
@Override
public String uploadInputStream(InputStream is, String fileName) {
// Zone.zone0:华东
// Zone.zone1:华北
// Zone.zone2:华南
// Zone.zoneNa0:北美
Configuration cfg = new Configuration(Region.autoRegion());
UploadManager uploadManager = new UploadManager(cfg);
try {
String upToken = auth.uploadToken(this.bucket);
Response response = uploadManager.put(is, this.newFileName, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
return putRet.key;
} catch (QiniuException ex) {
throw new ServiceException("[" + this.storageType + "]文件上传失败:" + ex.error());
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class QiniuApiClient method downloadFileStream.
@Override
public InputStream downloadFileStream(String key) {
try {
String encodedFileName = URLEncoder.encode(key, "utf-8").replace("+", "%20");
String publicUrl = String.format("%s/%s", this.newFileUrl, encodedFileName);
long expireInSeconds = 3600;
String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
return FileUtil.getInputStreamByUrl(finalUrl, "");
} catch (UnsupportedEncodingException e) {
throw new ServiceException("[" + this.storageType + "]下载文件发生异常:" + e);
}
}
use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.
the class UpaiyunOssApiClient method uploadInputStream.
@Override
public String uploadInputStream(InputStream is, String imageUrl) {
// 切换 API 接口的域名接入点,默认为自动识别接入点
upaiManager.setApiDomain(UpaiManager.ED_AUTO);
// 设置连接超时时间,默认为30秒
upaiManager.setTimeout(60);
try {
Map<String, String> param = new HashMap<>();
final Response response = upaiManager.writeFile(this.newFileName, is, param);
if (!response.isSuccessful()) {
throw new ServiceException("[" + this.storageType + "]文件上传失败.");
}
return this.newFileName;
} catch (IOException ex) {
throw new ServiceException("[" + this.storageType + "]文件上传失败:" + ex.getMessage());
}
}
Aggregations