Search in sources :

Example 26 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class QiniuApiClient method init.

@Override
public QiniuApiClient init(FileProperties fileProperties) {
    final QiniuFileProperties qiniuFileProperties = fileProperties.getQiniu();
    String accessKey = qiniuFileProperties.getAccessKey();
    String secretKey = qiniuFileProperties.getSecretKey();
    this.bucket = qiniuFileProperties.getBucketName();
    String url = qiniuFileProperties.getUrl();
    checkDomainUrl(url);
    if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(bucket)) {
        throw new ServiceException("[" + this.storageType + "]尚未配置七牛云,文件上传功能暂时不可用!");
    }
    auth = Auth.create(accessKey, secretKey);
    return this;
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) QiniuFileProperties(com.zhouzifei.tool.config.QiniuFileProperties)

Example 27 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class FileUtil method down.

// 下载视频
public static void down(InputStream ins, String saveFile) {
    try (InputStream inputStream = StreamUtil.clone(ins)) {
        int length = inputStream.available();
        FileOutputStream fs = new FileOutputStream(saveFile);
        byte[] buffer = new byte[1024];
        int i = 0, j = 0;
        int byteRead;
        while ((byteRead = inputStream.read(buffer)) != -1) {
            i++;
            fs.write(buffer, 0, byteRead);
            if (i % 500 == 0) {
                j++;
                File file2 = new File(saveFile);
                // 控制输出小数点后的位数
                DecimalFormat df = new DecimalFormat("#.##");
                float f = (file2.length() / (float) length) * 100;
                System.out.print("已下载:" + df.format(f) + "%\t\t");
                if (j % 5 == 0) {
                    log.info("下载完成");
                }
            }
        }
        log.info("\n已下载:100.00%");
        ins.close();
        fs.close();
    } catch (Exception e) {
        throw new ServiceException("9999999", "文件下载失败", e);
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) DecimalFormat(java.text.DecimalFormat) VirtualFile(com.zhouzifei.tool.dto.VirtualFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ServiceException(com.zhouzifei.tool.common.ServiceException)

Example 28 with ServiceException

use of com.zhouzifei.tool.common.ServiceException in project simpleFS by shengdingbox.

the class StreamUtil method clone.

/**
 * 复制InputStream
 *
 * @param is InputStream
 * @return
 */
public static InputStream clone(InputStream is) {
    if (null == is) {
        throw new ServiceException("无法获取文件流,文件不可用!");
    }
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) > -1) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        return new ByteArrayInputStream(baos.toByteArray());
    } catch (IOException e) {
        throw new ServiceException("无法复制当前文件流!{}", e.getMessage());
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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