use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.
the class ImageDownloadUtil method saveToCloudStorage.
/**
* 将网络图片转存到云存储中
*
* @param imgUrl 网络图片地址
* @param referer 为了预防某些网站做了权限验证,不加referer可能会403
*/
public static String saveToCloudStorage(String imgUrl, String referer) {
log.debug("download img >> %s", imgUrl);
String res = null;
try (InputStream is = getInputStreamByUrl(imgUrl, referer)) {
FileUploader uploader = new GlobalFileUploader();
VirtualFile file = uploader.upload(is, FileUploadType.SIMPLE.getPath(), imgUrl, false);
res = file.getFullFilePath();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new GlobalFileException(e.getMessage());
}
return res;
}
use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.
the class ExceptionHandleController method handle.
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResponseVO handle(Throwable e) {
if (e instanceof ZhydException || e instanceof GlobalFileException) {
return ResultUtil.error(e.getMessage());
}
if (e instanceof UndeclaredThrowableException) {
e = ((UndeclaredThrowableException) e).getUndeclaredThrowable();
}
ResponseStatus responseStatus = ResponseStatus.getResponseStatus(e.getMessage());
if (responseStatus != null) {
log.error(responseStatus.getMessage());
return ResultUtil.error(responseStatus.getCode(), responseStatus.getMessage());
}
// 打印异常栈
e.printStackTrace();
return ResultUtil.error(CommonConst.DEFAULT_ERROR_CODE, ResponseStatus.ERROR.getMessage());
}
use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.
the class StreamUtil method clone.
/**
* 复制InputStream
*
* @param is InputStream
* @return
*/
public static InputStream clone(InputStream is) {
if (null == is) {
throw new GlobalFileException("无法获取文件流,文件不可用!");
}
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 GlobalFileException("无法复制当前文件流!", e);
}
}
use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.
the class BaseApiClient method createNewFileName.
void createNewFileName(String key, String pathPrefix) {
this.suffix = FileUtil.getSuffix(key);
if (!FileUtil.isPicture(this.suffix)) {
throw new GlobalFileException("[" + this.storageType + "] 非法的图片文件[" + key + "]!目前只支持以下图片格式:[jpg, jpeg, png, gif, bmp]");
}
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
this.newFileName = pathPrefix + (fileName + this.suffix);
}
use of com.zyd.blog.file.exception.GlobalFileException in project OneBlog by zhangyd-c.
the class BaseApiClient method uploadImg.
@Override
public VirtualFile uploadImg(File file) {
this.check();
if (file == null) {
throw new QiniuApiException("[" + this.storageType + "]文件上传失败:文件不可为空");
}
try {
InputStream is = new BufferedInputStream(new FileInputStream(file));
VirtualFile res = this.uploadImg(is, "temp" + FileUtil.getSuffix(file));
VirtualFile imageInfo = ImageUtil.getInfo(file);
return res.setSize(imageInfo.getSize()).setOriginalFileName(file.getName()).setWidth(imageInfo.getWidth()).setHeight(imageInfo.getHeight());
} catch (FileNotFoundException e) {
throw new GlobalFileException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
}
}
Aggregations