Image-Handling JPA+Upload
Explain ImageController
Move #loadImage from ImageController to BookController
Book entity: add LOB field
Insert example via LOAD_FILE
book/edit.html: Add input to form + change to multipart
Controller anpassen (loadImage)
MultipartFileToByteArrayConverter + WebConfig
application.properties: Adapt file POST limits
JpegValidator
SHOW VARIABLES WHERE variable_name= ' secure_file_priv' ;
UPDATE book SET image= LOAD_FILE(' /var/lib/mysql-files/Refactoring.jpeg' );
# File Upload limits
spring.servlet.multipart.max-file-size =1024KB
spring.servlet.multipart.max-request-size =1024KB
@ Documented
@ Constraint (validatedBy = JPEGValidator .class )
@ Target ( { ElementType .PARAMETER , ElementType .FIELD })
@ Retention (RetentionPolicy .RUNTIME )
public @interface JPEGConstraint {
String message () default "Bild hat kein JPEG-Format!" ;
Class <?>[] groups () default {};
Class <? extends Payload >[] payload () default {};
}
public class JPEGValidator implements
ConstraintValidator <JPEGConstraint , byte []> {
private ImageService imageService ;
@ Autowired
public void setImageService (ImageService imageService ) {
this .imageService = imageService ;
}
@ Override
public void initialize (JPEGConstraint image ) {
}
@ Override
public boolean isValid (byte [] imageArray ,
ConstraintValidatorContext validatorContext ) {
// initialize result variable
boolean valid = false ;
// if image was passed
if (imageArray .length > 0 ) {
valid = imageService .getImageType (imageArray ) == "JPEG" ;
} else {
valid = true ;
}
return valid ;
}
}