Administrator
2018-11-13 31e4c76d6bd785af9a6b5167e8412fe93ccb0a9e
Jsonp上传图片(通用)
1个文件已修改
41 ■■■■■ 已修改文件
fanli/src/main/java/com/yeshi/fanli/controller/admin/UploadController.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/controller/admin/UploadController.java
@@ -198,4 +198,45 @@
            
    }
    
    /**
     * 上传图片jsonp
     * @param file
     * @param out
     */
    @RequestNoLogin()
    @RequestMapping(value = "uploadPicture")
    public void uploadPicture(@RequestParam("file") CommonsMultipartFile file, Long uid,
            HttpServletResponse response, PrintWriter out) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        AdminUser admin = adminUserService.selectByPrimaryKey(uid);
        if (admin == null) {
            out.print(JsonUtil.loadFalseResult("当前账户验证失败"));
            return;
        }
        if (file == null) {
            out.print(JsonUtil.loadFalseResult("上传文件为空"));
            return;
        }
        try {
            InputStream inputStream = file.getInputStream();
            String contentType = file.getContentType();
            String type = contentType.substring(contentType.indexOf("/") + 1);
            // 上传文件相对位置
            String fileUrl=UUID.randomUUID().toString().replace("-", "") + "." + type;
            String uploadPath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
            out.print(JsonUtil.loadTrueResult(uploadPath));
        } catch (IOException e) {
            out.print(JsonUtil.loadFalseResult("上传图片失败"));
            e.printStackTrace();
        }
    }
}