Search in sources :

Example 1 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class HttpUtils method downloadFile.

public static String downloadFile(String fileURL, String saveDir) {
    HttpURLConnection httpConn = null;
    FileOutputStream outputStream = null;
    try {
        URL url = new URL(fileURL);
        httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            if (disposition != null) {
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10, disposition.length() - 1);
                }
            } else {
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
            }
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + File.separator + fileName;
            outputStream = new FileOutputStream(saveFilePath);
            int bytesRead = -1;
            byte[] buffer = new byte[2048];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
            return saveFilePath;
        } else {
            throw new JeesuiteBaseException(responseCode, "下载失败");
        }
    } catch (IOException e) {
        throw new JeesuiteBaseException(500, "下载失败", e);
    } finally {
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (Exception e2) {
        }
        try {
            if (httpConn != null)
                httpConn.disconnect();
        } catch (Exception e2) {
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 2 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class MinioProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        String bucketName = object.getBucketName();
        if (StringUtils.isEmpty(bucketName)) {
            throw new JeesuiteBaseException("BucketName 不能为空");
        }
        String fileKey = object.getFileKey();
        InputStream inputStream = object.getInputStream();
        byte[] objectBytes = object.getBytes();
        ObjectWriteResponse objectWriteResponse = null;
        long size = 0;
        if (object.getFile() != null) {
            objectWriteResponse = minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).filename(object.getFile().getAbsolutePath()).object(fileKey).contentType(object.getMimeType()).build());
            size = object.getFile().length();
        } else if (objectBytes != null) {
            byte[] bytes = objectBytes;
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(bis, bytes.length, -1).build());
            size = bytes.length;
            bis.close();
        } else if (inputStream != null) {
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(inputStream, inputStream.available(), -1).build());
            size = inputStream.available();
        } else {
            throw new JeesuiteBaseException("upload object is NULL");
        }
        if (objectWriteResponse != null) {
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (JeesuiteBaseException jbex) {
        throw jbex;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : ObjectWriteResponse(io.minio.ObjectWriteResponse) CUploadResult(com.jeesuite.cos.CUploadResult) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 3 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class QiniuProvider method deleteBucket.

@Override
public void deleteBucket(String bucketName) {
    bucketName = buildBucketName(bucketName);
    String path = "/drop/" + bucketName + "\n";
    String accessToken = auth.sign(path);
    String url = "http://rs.qiniu.com/drop/" + bucketName;
    Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/x-www-form-urlencoded").addHeader("Authorization", "QBox " + accessToken).build();
    okhttp3.Response re = null;
    try {
        re = httpClient.newCall(request).execute();
        if (!re.isSuccessful()) {
            throw new JeesuiteBaseException(re.code(), re.message());
        }
    } catch (IOException e) {
        throw new JeesuiteBaseException(e.getMessage());
    }
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Request(okhttp3.Request) IOException(java.io.IOException)

Example 4 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class QiniuProvider method processQiniuException.

private void processQiniuException(String bucketName, QiniuException e) {
    Response r = e.response;
    if (e.code() == 631) {
        throw new JeesuiteBaseException(404, "bucketName[" + bucketName + "]不存在");
    }
    if (e.code() == 614) {
        throw new JeesuiteBaseException(406, "bucketName[" + bucketName + "]已存在");
    }
    if (e.code() == 612) {
        throw new JeesuiteBaseException(404, "资源不存在");
    }
    String message;
    try {
        message = r.bodyString();
    } catch (Exception e2) {
        message = r.toString();
    }
    throw new JeesuiteBaseException(message);
}
Also used : Response(com.qiniu.http.Response) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) QiniuException(com.qiniu.common.QiniuException) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 5 with JeesuiteBaseException

use of com.jeesuite.common.JeesuiteBaseException in project jeesuite-libs by vakinge.

the class QiniuProvider method buildBucketUrlPrefix.

protected String buildBucketUrlPrefix(String buketName) {
    String rs = "";
    String path = "/v6/domain/list?tbl=" + buketName + "\n";
    String accessToken = auth.sign(path);
    String url = "http://api.qiniu.com/v6/domain/list?tbl=" + buketName;
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).addHeader("Host", "api.qiniu.com").addHeader("Content-Type", "application/x-www-form-urlencoded").addHeader("Authorization", "QBox " + accessToken).build();
    okhttp3.Response re = null;
    try {
        re = client.newCall(request).execute();
        if (re.isSuccessful() == true) {
            String[] reArr = JsonUtils.toObject(re.body().string(), String[].class);
            if (reArr.length > 0) {
                rs = reArr[0];
                if (!rs.endsWith(FilePathHelper.DIR_SPLITER)) {
                    rs = rs.concat(FilePathHelper.DIR_SPLITER);
                }
            }
        } else {
            throw new JeesuiteBaseException(re.message());
        }
    } catch (IOException e) {
        throw new JeesuiteBaseException(e.getMessage());
    }
    return rs;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)41 IOException (java.io.IOException)14 Request (okhttp3.Request)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 CObjectMetadata (com.jeesuite.cos.CObjectMetadata)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SignatureException (java.security.SignatureException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CUploadResult (com.jeesuite.cos.CUploadResult)2 WrapperResponseEntity (com.jeesuite.springweb.model.WrapperResponseEntity)2 COSObject (com.qcloud.cos.model.COSObject)2 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)2 QiniuException (com.qiniu.common.QiniuException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2