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) {
}
}
}
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;
}
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());
}
}
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);
}
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;
}
Aggregations