HTML and CSS Reference
In-Depth Information
public String uploadPhoto() throws IOException {
// Uploading file. You don't have to do anything here, but you could
// use it for post processing. Don't use this method for validating
// the uploaded file.
byte[] photoContents = IOUtils.toByteArray(photo.getInputStream());
userProfileService.savePhoto(userProfile, photoContents);
FacesContext.getCurrentInstance().addMessage("frm-photo-upload",
new FacesMessage(FacesMessage.SEVERITY_INFO, "Photo uploaded successfully",
"Name: " + photo.getName() + " Size: " + (photo.getSize()/1024) + " KB"));
return "/photo-uploaded";
}
/**
* Validator for checking that the file uploaded is a photo and that the file
* size is less than 2MB.
*/
public void validatePhoto(FacesContext ctx, UIComponent comp, Object value) {
// List of possible validation errors
List<FacesMessage> msgs = new ArrayList<>();
// Retrieve the uploaded file from passed value object
Part photo = (Part)value;
// Ensure that the file is an image
if (!"image/".startsWith(file.getContentType())) {
msgs.add(new FacesMessage("The uploaded file must be an image"));
}
// Ensure that the file is less than 2 MB
if (file.getSize() > 2048) {
msgs.add(new FacesMessage("The uploaded file is larger than 2MB"));
}
// Determine if a validation exception should be thrown
if (!msgs.isEmpty()) {
throw new ValidatorException(msgs);
}
}
public Part getPhoto() {
return photo;
}
public void setPhoto(Part photo) {
this.photo = photo;
}
public UserProfile getUserProfile() {
return this.userProfile;
}
Search WWH ::




Custom Search