use of cn.exrick.common.exception.XmallUploadException in project xmall by Exrick.
the class ImageController method uploadFile.
@RequestMapping(value = "/image/imageUpload", method = RequestMethod.POST)
@ApiOperation(value = "WebUploader图片上传")
public Result<Object> uploadFile(@RequestParam("file") MultipartFile files, HttpServletRequest request) {
String imagePath = null;
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/upload") + "\\" + QiniuUtil.renamePic(files.getOriginalFilename());
// 转存文件
try {
// 保存至服务器
File file = new File((filePath));
files.transferTo(file);
// 上传七牛云服务器
imagePath = QiniuUtil.qiniuUpload(filePath);
if (imagePath.contains("error")) {
throw new XmallUploadException("上传失败");
}
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
return new ResultUtil<Object>().setData(imagePath);
}
use of cn.exrick.common.exception.XmallUploadException in project xmall by Exrick.
the class QiniuUtil method qiniuInputStreamUpload.
/**
* 文件流上传
* @param file
* @param key 文件名
* @return
*/
public static String qiniuInputStreamUpload(FileInputStream file, String key) {
// 构造一个带指定Zone对象的配置类 zone2华南
Configuration cfg = new Configuration(Zone.zone2());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file, key, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return origin + putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
log.warn(r.toString());
try {
return r.bodyString();
} catch (QiniuException e) {
throw new XmallUploadException(e.toString());
}
}
}
use of cn.exrick.common.exception.XmallUploadException in project xmall by Exrick.
the class QiniuUtil method qiniuUpload.
public static String qiniuUpload(String filePath) {
// 构造一个带指定Zone对象的配置类 zone2华南
Configuration cfg = new Configuration(Zone.zone2());
UploadManager uploadManager = new UploadManager(cfg);
String localFilePath = filePath;
// 默认不指定key的情况下,以文件内容的hash值作为文件名
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(localFilePath, key, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return origin + putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
log.warn(r.toString());
try {
return r.bodyString();
} catch (QiniuException e) {
throw new XmallUploadException(e.toString());
}
}
}
Aggregations