Search in sources :

Example 6 with ServiceException

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());
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ServiceException(com.zhouzifei.tool.common.ServiceException) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BASE64Encoder(sun.misc.BASE64Encoder) IOException(java.io.IOException)

Example 7 with ServiceException

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;
}
Also used : LocalFileProperties(com.zhouzifei.tool.config.LocalFileProperties) ServiceException(com.zhouzifei.tool.common.ServiceException)

Example 8 with ServiceException

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());
    }
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) Configuration(com.qiniu.storage.Configuration) ServiceException(com.zhouzifei.tool.common.ServiceException) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) UploadManager(com.qiniu.storage.UploadManager)

Example 9 with ServiceException

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);
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with ServiceException

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());
    }
}
Also used : Response(okhttp3.Response) ServiceException(com.zhouzifei.tool.common.ServiceException) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

ServiceException (com.zhouzifei.tool.common.ServiceException)28 IOException (java.io.IOException)12 Connection (com.zhouzifei.tool.common.fastdfs.pool.Connection)4 SdkClientException (com.amazonaws.SdkClientException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashMap (java.util.HashMap)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)1 DefaultBceCredentials (com.baidubce.auth.DefaultBceCredentials)1 BosClient (com.baidubce.services.bos.BosClient)1 BosClientConfiguration (com.baidubce.services.bos.BosClientConfiguration)1 ObsClient (com.obs.services.ObsClient)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1