Uploading files in ADF can be easily done using a component "inputFile".
Here in this blog I am sharing my experience of uploading an image file in ADF.
I will make following checks for the image :
1. Image with the extension other than PNG not allowed
2. Size of the image should not be more than 150*60
3. Check the directory exist, if not create directory.
Code snippet below :
1. Code for jsff :
<af:panelGroupLayout id="pgl1" layout="horizontal">
<af:inputFile id="inputFile"
required="true" label="upload"
requiredMessageDetail="Please select a file to upload"
value="#{pageFlowScope.uploadImage.uploadedFile}"
binding="#{pageFlowScope.uploadImage.inputFileBind}"/>
<af:spacer id="sp1" width="10px"/>
<af:commandImageLink text="Upload" id="upload"
partialTriggers="inputFile"
actionListener="#{pageFlowScope.uploadBanner.onUploadImage}"/>
</af:panelGroupLayout>
2. Code of Java Class - UploadImage.java
package com.upload.image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.jbo.domain.Number;
import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.model.UploadedFile;
/**
* Upload the image
*/
public class UploadImage {
private UploadedFile uploadedFile = null;
private RichInputFile inputFileBind;
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setInputFileBind(RichInputFile inputFileBind) {
this.inputFileBind = inputFileBind;
}
public RichInputFile getInputFileBind() {
return inputFileBind;
}
/**
* On click of upload button
* @param actionEvent
*/
public void onUploadImage(ActionEvent actionEvent) {
System.out.println("Entry - [UploadImage : onUploadImage]");
InputStream inputStream = null;
String image = null;
try {
System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image type : " +
uploadedFile.getContentType());
inputStream = uploadedFile.getInputStream();
System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image InputStream : " +
uploadedFile.getInputStream());
image = uploadedFile.getFilename();
//Condition to check whether the image is png or not
if ("image/png".equalsIgnoreCase(uploadedFile.getContentType())) {
System.out.println("[UploadImage : onUploadImage] - File Name : " +
uploadedFile.getFilename());
System.out.println("[UploadImage : onUploadImage] - image : " + image);
//Method call to upload png
uploadPngFile(image, inputStream);
} else {
showADFDefaultPopup("Only .png file is allowed");
}
showADFDefaultPopup(image + " image has been uploaded successfully");
} catch (IOException e) {
e.getMessage();
}
System.out.println("Exit - [UploadImage : onUploadImage]");
}
private void uploadPngFile(String image, InputStream inputStream) throws IOException {
String pathToStore = "C://myImages";
Integer definedHeight = 150;
Integer definedWidth = 60;
//Reading image from inputStream
BufferedImage bufferedImage = ImageIO.read(inputStream);
if (checkDimensions(bufferedImage, definedHeight, definedWidth)) {
File outputFile = new File(pathToStore + image);
File directory = new File(pathToStore);
if (directory.isDirectory()) {
ImageIO.write(bufferedImage, "PNG", outputFile);
} else {
if (directory.mkdirs()) {
ImageIO.write(bufferedImage, "PNG", outputFile);
System.out.println("[UploadImage : onUploadImage] - Multiple file directory created");
} else {
System.out.println("[UploadImage : onUploadImage] - Failed while creating multiple files");
}
}
} else {
showADFDefaultPopup("Maximum dimension allowed is " + definedHeight + "*" +
definedWidth + ".Please check the image used");
}
}
private boolean checkDimensions(BufferedImage bufferedImage, int definedHeight,
int definedWidth) {
boolean accepted = false;
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
System.out.println("[UploadImage : checkDimensions] - height : " + height);
System.out.println("[UploadImage : checkDimensions] - width : " + width);
if (definedHeight > height && definedWidth > width) {
accepted = true;
} else {
accepted = false;
}
return accepted;
}
/**
* Shows ADF default pop-up in the UI
* @param text
*/
private static void showADFDefaultPopup(String text) {
FacesContext fcntx = FacesContext.getCurrentInstance();
FacesMessage popup = new FacesMessage(FacesMessage.SEVERITY_INFO, "", text);
fcntx.addMessage(null, popup);
}
}
*Comments and feedback are most welcomed, Thanks.
Here in this blog I am sharing my experience of uploading an image file in ADF.
I will make following checks for the image :
1. Image with the extension other than PNG not allowed
2. Size of the image should not be more than 150*60
3. Check the directory exist, if not create directory.
Code snippet below :
1. Code for jsff :
<af:panelGroupLayout id="pgl1" layout="horizontal">
<af:inputFile id="inputFile"
required="true" label="upload"
requiredMessageDetail="Please select a file to upload"
value="#{pageFlowScope.uploadImage.uploadedFile}"
binding="#{pageFlowScope.uploadImage.inputFileBind}"/>
<af:spacer id="sp1" width="10px"/>
<af:commandImageLink text="Upload" id="upload"
partialTriggers="inputFile"
actionListener="#{pageFlowScope.uploadBanner.onUploadImage}"/>
</af:panelGroupLayout>
2. Code of Java Class - UploadImage.java
package com.upload.image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.imageio.ImageIO;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.jbo.domain.Number;
import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.model.UploadedFile;
/**
* Upload the image
*/
public class UploadImage {
private UploadedFile uploadedFile = null;
private RichInputFile inputFileBind;
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setInputFileBind(RichInputFile inputFileBind) {
this.inputFileBind = inputFileBind;
}
public RichInputFile getInputFileBind() {
return inputFileBind;
}
/**
* On click of upload button
* @param actionEvent
*/
public void onUploadImage(ActionEvent actionEvent) {
System.out.println("Entry - [UploadImage : onUploadImage]");
InputStream inputStream = null;
String image = null;
try {
System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image type : " +
uploadedFile.getContentType());
inputStream = uploadedFile.getInputStream();
System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image InputStream : " +
uploadedFile.getInputStream());
image = uploadedFile.getFilename();
//Condition to check whether the image is png or not
if ("image/png".equalsIgnoreCase(uploadedFile.getContentType())) {
System.out.println("[UploadImage : onUploadImage] - File Name : " +
uploadedFile.getFilename());
System.out.println("[UploadImage : onUploadImage] - image : " + image);
//Method call to upload png
uploadPngFile(image, inputStream);
} else {
showADFDefaultPopup("Only .png file is allowed");
}
showADFDefaultPopup(image + " image has been uploaded successfully");
} catch (IOException e) {
e.getMessage();
}
System.out.println("Exit - [UploadImage : onUploadImage]");
}
private void uploadPngFile(String image, InputStream inputStream) throws IOException {
String pathToStore = "C://myImages";
Integer definedHeight = 150;
Integer definedWidth = 60;
//Reading image from inputStream
BufferedImage bufferedImage = ImageIO.read(inputStream);
if (checkDimensions(bufferedImage, definedHeight, definedWidth)) {
File outputFile = new File(pathToStore + image);
File directory = new File(pathToStore);
if (directory.isDirectory()) {
ImageIO.write(bufferedImage, "PNG", outputFile);
} else {
if (directory.mkdirs()) {
ImageIO.write(bufferedImage, "PNG", outputFile);
System.out.println("[UploadImage : onUploadImage] - Multiple file directory created");
} else {
System.out.println("[UploadImage : onUploadImage] - Failed while creating multiple files");
}
}
} else {
showADFDefaultPopup("Maximum dimension allowed is " + definedHeight + "*" +
definedWidth + ".Please check the image used");
}
}
private boolean checkDimensions(BufferedImage bufferedImage, int definedHeight,
int definedWidth) {
boolean accepted = false;
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
System.out.println("[UploadImage : checkDimensions] - height : " + height);
System.out.println("[UploadImage : checkDimensions] - width : " + width);
if (definedHeight > height && definedWidth > width) {
accepted = true;
} else {
accepted = false;
}
return accepted;
}
/**
* Shows ADF default pop-up in the UI
* @param text
*/
private static void showADFDefaultPopup(String text) {
FacesContext fcntx = FacesContext.getCurrentInstance();
FacesMessage popup = new FacesMessage(FacesMessage.SEVERITY_INFO, "", text);
fcntx.addMessage(null, popup);
}
}
*Comments and feedback are most welcomed, Thanks.
No comments:
Post a Comment