解决org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException 图片访问接口

org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededExceptio

// 设置单个文件大小
spring.servlet.multipart.max-file-size= 10MB
// 设置单次请求文件的总大小
spring.servlet.multipart.max-request-size= 10MB
@Bean
public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    //允许上传的文件最大值
    factory.setMaxFileSize("50MB"); //KB,MB
    /// 设置总上传数据总大小
    factory.setMaxRequestSize("50MB");
    return factory.createMultipartConfig();
}

案例

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {}, allowCredentials = "true")
public class Image {


    @PostMapping("/Image")
    public Object getImage(MultipartFile filebody,String type) {
        String token = GetToken();
        String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=" + token + "&media_type=" + type + "&attachment_type=1";
        MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("filebody", new FileSystemResource(convert(filebody)));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
        RestTemplate restTemplate = new RestTemplate();
        JSONObject s = restTemplate.postForObject(url, requestEntity, JSONObject.class);
        String errmsg = s.getString("errcode");
        String rootPath = System.getProperty("user.dir");
        String u = "/temp_image";
        String root = rootPath + u;
        File file = new File(root);
        deleteDir(file);

        if ("0".equals(errmsg)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("errcode", "0");
            jsonObject.put("type", type);
            jsonObject.put("media_id", s.getString("media_id"));
            jsonObject.put("created_at", System.currentTimeMillis());
            jsonObject.put("errmsg", "素材上传成功");
            return jsonObject;
        } else {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("errcode", 418);
            jsonObject.put("errmsg", "内容格式错误");
            return jsonObject;
        }
    }

    // 遍历删除方法
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();

    }

    // 创建内部文件夹
    public static File convert(MultipartFile file) {
        File convFile = new File("temp_image", file.getOriginalFilename());
        if (!convFile.getParentFile().exists()) {
            System.out.println("mkdir:" + convFile.getParentFile().mkdirs());
        }
        try {
            convFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(convFile);
            fos.write(file.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return convFile;
    }

    // 获取 Token 
    private String GetToken() {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
        Map<String, Object> map = new HashMap<>();
        map.put("corpid", "xxxxxxxxxxxxxxxxxx");
        map.put("corpsecret", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");
        HttpEntity<Map> entity = new HttpEntity<>(map);
        RestTemplate restTemplate = new RestTemplate();
        JSONObject jsonToken = restTemplate.postForObject(url, entity, JSONObject.class);
        System.out.println("json = " + jsonToken.toString());
        String token = jsonToken.getString("access_token");
        System.out.println("token=" + token);
        return token;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值