Search in sources :

Example 1 with XmallUploadException

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);
}
Also used : XmallUploadException(cn.exrick.common.exception.XmallUploadException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) XmallUploadException(cn.exrick.common.exception.XmallUploadException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with XmallUploadException

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());
        }
    }
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) XmallUploadException(cn.exrick.common.exception.XmallUploadException) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) Gson(com.google.gson.Gson) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) UploadManager(com.qiniu.storage.UploadManager)

Example 3 with XmallUploadException

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());
        }
    }
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) XmallUploadException(cn.exrick.common.exception.XmallUploadException) Configuration(com.qiniu.storage.Configuration) Auth(com.qiniu.util.Auth) Gson(com.google.gson.Gson) DefaultPutRet(com.qiniu.storage.model.DefaultPutRet) UploadManager(com.qiniu.storage.UploadManager)

Aggregations

XmallUploadException (cn.exrick.common.exception.XmallUploadException)3 Gson (com.google.gson.Gson)2 QiniuException (com.qiniu.common.QiniuException)2 Response (com.qiniu.http.Response)2 Configuration (com.qiniu.storage.Configuration)2 UploadManager (com.qiniu.storage.UploadManager)2 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)2 Auth (com.qiniu.util.Auth)2 ApiOperation (io.swagger.annotations.ApiOperation)1 File (java.io.File)1 IOException (java.io.IOException)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1