2016-06-13 3 views
1

클라이언트 측에서 이미지를 너비와 높이 250px로 잘라냅니다. 이미지를 base64로 보냅니다. Cropper의 결과는 항상 base64이며,이 여러 부분으로 나누고 싶은 경우에 몇 가지 문제가 있습니다.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); 

} 

답변

0

Base64 인코딩은 3 바이트의 이진 데이터마다 4 바이트를 사용합니다. 이것은 아마도 괜찮을 것입니다 - 단지 33 % 더 많은 데이터입니다.

나는이 데이터를 요청 본문 대신에 요청 매개 변수로 전달하는 것에 더 관심이 있습니다. this question에서 볼 수 있듯이 단일 요청 매개 변수에서 처리 할 수있는 데이터 서버 및 클라이언트의 양에 대한 제한이있는 경우가 있습니다. 대신 Spring의 @RequestBody을 사용하는 것이 좋습니다.

+0

POST 매개 변수는 요청 본문에 전달됩니다. – EJP

관련 문제