Search in sources :

Example 1 with KindEditorResult

use of cn.exrick.common.pojo.KindEditorResult in project xmall by Exrick.

the class ImageController method kindeditor.

@RequestMapping(value = "/kindeditor/imageUpload", method = RequestMethod.POST)
@ApiOperation(value = "KindEditor图片上传")
public KindEditorResult kindeditor(@RequestParam("imgFile") MultipartFile files, HttpServletRequest request) {
    KindEditorResult kindEditorResult = new KindEditorResult();
    // 文件保存路径
    String filePath = request.getSession().getServletContext().getRealPath("/upload") + "\\" + QiniuUtil.renamePic(files.getOriginalFilename());
    // 检查文件
    String message = QiniuUtil.isValidImage(request, files);
    if (!message.equals("valid")) {
        kindEditorResult.setError(1);
        kindEditorResult.setMessage(message);
        return kindEditorResult;
    }
    // 转存文件
    try {
        // 保存至服务器
        File file = new File((filePath));
        files.transferTo(file);
        // 上传七牛云服务器
        String imagePath = QiniuUtil.qiniuUpload(filePath);
        if (imagePath.contains("error")) {
            kindEditorResult.setError(1);
            kindEditorResult.setMessage("上传失败");
            return kindEditorResult;
        }
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
        kindEditorResult.setError(0);
        kindEditorResult.setUrl(imagePath);
        return kindEditorResult;
    } catch (IOException e) {
        e.printStackTrace();
    }
    kindEditorResult.setError(1);
    kindEditorResult.setMessage("上传失败");
    return kindEditorResult;
}
Also used : KindEditorResult(cn.exrick.common.pojo.KindEditorResult) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

KindEditorResult (cn.exrick.common.pojo.KindEditorResult)1 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