In this post we will see how to upload images in Oracle ADF application.
To upload a file don't forget to set UsesUpload property of form of jspx page to true.
Lets start with uploading a image/file from client to the server.
ADF rich faces provide the tag af:inputFile to allow uploading of data.
Define a variable Type UploadedFile and create its accessors
private UploadedFile _file;
public void setFile(UploadedFile _file) {
this._file = _file;
}
public UploadedFile getFile() {
return _file;
}
And bind it with inputFile component's value property
In this part, we will see how to upload a image/file from the client to and store image in a blob column in a database.
We have used button action to upload file
Following are codes to upload Image in database blob column
public String UploadImageAction() {
UploadedFile myfile = this.getFile();
if (myfile != null) {
if (myfile.getContentType().equalsIgnoreCase("image/jpeg") || myfile.getContentType().equalsIgnoreCase("image/png") || myfile.getContentType().equalsIgnoreCase("image/gif")
){
AppImageAMImpl am = (AppImageAMImpl)resolvElDC("AppImageAMDataControl");
ViewObjectImpl v = am.getAppImgAdd();
AppImgAddVORowImpl currRow = (AppImgAddVORowImpl)v.getCurrentRow();
try {
//to save image in Blob column in database directory..............
currRow.setImgBlob(createBlobDomain(getFile()));
} catch (Exception ex) {
}
}
}
setFile(null);
return null;
}
private BlobDomain createBlobDomain(UploadedFile file) {
InputStream in = null;
BlobDomain blobDomain = null;
OutputStream out = null;
try {
in = file.getInputStream();
blobDomain = new BlobDomain();
out = blobDomain.getBinaryOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.fillInStackTrace();
}
return blobDomain;
}
2.) In Actual path in server :-
In this part, we will see how to upload a image/file from the client to actual location in server.
public String UploadImageAction() {
UploadedFile myfile = this.getFile();
if (myfile != null) {
if (myfile.getContentType().equalsIgnoreCase("image/jpeg") || myfile.getContentType().equalsIgnoreCase("image/png")
|| myfile.getContentType().equalsIgnoreCase("image/gif")
){
String path = "D:\\Images\\";
if(myfile.getContentType().equalsIgnoreCase("image/jpeg")){
TypeVal=".jpeg";
}
else if(myfile.getContentType().equalsIgnoreCase("image/png")){
TypeVal=".png";
}
else if( myfile.getContentType().equalsIgnoreCase("image/gif")){
TypeVal=".gif";
}
AppItemImageAMImpl am = (AppItemImageAMImpl)resolvElDC("AppItemImageAMDataControl");
ViewObjectImpl v = am.getAppItmImgAdd();
AppItmImgAddVORowImpl currRow = (AppItmImgAddVORowImpl)v.getCurrentRow();
String ImgId = currRow.getImgId();
try {
InputStream inputStream = myfile.getInputStream();
BufferedImage input = ImageIO.read(inputStream);
//to save image in another directory..............
File outputFile = new File(path + ImgId + TypeVal);
ImageIO.write(input,type, outputFile);
} catch (Exception ex) {
// handle exception
}
}
}
setFile(null);
return null;
}
To upload a file don't forget to set UsesUpload property of form of jspx page to true.
Lets start with uploading a image/file from client to the server.
ADF rich faces provide the tag af:inputFile to allow uploading of data.
Define a variable Type UploadedFile and create its accessors
private UploadedFile _file;
public void setFile(UploadedFile _file) {
this._file = _file;
}
public UploadedFile getFile() {
return _file;
}
And bind it with inputFile component's value property
<af:inputFile label="Upload Image" id="if1" value="#{ImageUploadBean.file}" />
In this we will see 2 places where we can uploading image depending on our requirement :-
1.)
In Database(BLOB)
:-
In this we will see 2 places where we can uploading image depending on our requirement :-
In this part, we will see how to upload a image/file from the client to and store image in a blob column in a database.
We have used button action to upload file
Following are codes to upload Image in database blob column
public String UploadImageAction() {
UploadedFile myfile = this.getFile();
if (myfile != null) {
if (myfile.getContentType().equalsIgnoreCase("image/jpeg") || myfile.getContentType().equalsIgnoreCase("image/png") || myfile.getContentType().equalsIgnoreCase("image/gif")
){
AppImageAMImpl am = (AppImageAMImpl)resolvElDC("AppImageAMDataControl");
ViewObjectImpl v = am.getAppImgAdd();
AppImgAddVORowImpl currRow = (AppImgAddVORowImpl)v.getCurrentRow();
try {
//to save image in Blob column in database directory..............
currRow.setImgBlob(createBlobDomain(getFile()));
} catch (Exception ex) {
}
}
}
setFile(null);
return null;
}
private BlobDomain createBlobDomain(UploadedFile file) {
InputStream in = null;
BlobDomain blobDomain = null;
OutputStream out = null;
try {
in = file.getInputStream();
blobDomain = new BlobDomain();
out = blobDomain.getBinaryOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.fillInStackTrace();
}
return blobDomain;
}
2.) In Actual path in server :-
In this part, we will see how to upload a image/file from the client to actual location in server.
public String UploadImageAction() {
UploadedFile myfile = this.getFile();
if (myfile != null) {
if (myfile.getContentType().equalsIgnoreCase("image/jpeg") || myfile.getContentType().equalsIgnoreCase("image/png")
|| myfile.getContentType().equalsIgnoreCase("image/gif")
){
String path = "D:\\Images\\";
if(myfile.getContentType().equalsIgnoreCase("image/jpeg")){
TypeVal=".jpeg";
}
else if(myfile.getContentType().equalsIgnoreCase("image/png")){
TypeVal=".png";
}
else if( myfile.getContentType().equalsIgnoreCase("image/gif")){
TypeVal=".gif";
}
AppItemImageAMImpl am = (AppItemImageAMImpl)resolvElDC("AppItemImageAMDataControl");
ViewObjectImpl v = am.getAppItmImgAdd();
AppItmImgAddVORowImpl currRow = (AppItmImgAddVORowImpl)v.getCurrentRow();
String ImgId = currRow.getImgId();
try {
InputStream inputStream = myfile.getInputStream();
BufferedImage input = ImageIO.read(inputStream);
//to save image in another directory..............
File outputFile = new File(path + ImgId + TypeVal);
ImageIO.write(input,type, outputFile);
} catch (Exception ex) {
// handle exception
}
}
}
setFile(null);
return null;
}