BASE64上传 - 性能



在客户端我将图像裁剪到宽度和高度250px。我将图像发送为base64。Cropper的结果始终是base64,并且有一些问题,以防我想从这个多部分中提出。

我想知道将图像作为字符串值发送给控制器并管理此的性能,以防某人直接上传图像或直接使用API的任何情况。

是上传图像,管理和验证的正确方法吗?

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<?> upload(@RequestParam("imageValue") String imageValue, HttpServletRequest request) {
    try {
        String splitter = ";base64,";
        String[] splitted = imageValue.split(splitter);
        if (splitted.length != 2)
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        byte[] imageByte = Base64.decodeBase64(splitted[1]);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        BufferedImage image = ImageIO.read(bis);
        if (image == null || image.getHeight() > 250 || image.getWidth() > 250)
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        FooImageData fooImageData = new FooImageData ();
        fooImageData.setBase64(imageByte);
        fooImageData.setMimeType(splitted[0] + splitter);
        fooImageDataService.save(fooImageData);
        bis.close();
        return new ResponseEntity<>(HttpStatus.OK);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

base64编码使用4个字节的二进制数据使用4个字节。这可能是可以的 - 只有33%的数据。

我更关心将这些数据作为请求参数传递,而不是在请求的正文中。正如您在这个问题中看到的那样,有时在单个请求参数中可以处理多少数据服务器和客户端有限制。我会考虑使用Spring的@RequestBody

最新更新