Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to convert Byte array to Blob type in managed beans in java.

user9933754Jul 12 2012
Hi all,

I am struggling to find the solution for converting Byte array to Blob type in adf in managed beans.

The scenario is like from the jsf page I am uploading the text file that I need to save at the back end into the table into blob column.Am accessing the file as string in managed beans then am converting it into byte array.Now am not able to convert byte array into blob type.Here is my code in managed bean..

Blob blobContent;

public void UploadFile(ActionEvent actionEvent) throws Exception{

file=getFile();
System.out.println("the size of file......."+file);
if(file!=null){

int quoteid = 0;
String result = null;
String mimeType=file.getContentType();
System.out.println("mimetype...content type..."+mimeType);


if("text/plain".equalsIgnoreCase(mimeType)){
final char[] buffer = new char[0x10000];

InputStream is = null;
is = file.getInputStream();
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(is, "UTF-8");
int read;
do {
read = in.read(buffer, 0, buffer.length);
if (read>0) {
out.append(buffer, 0, read);
}
} while (read>=0);
result = out.toString();
System.out.println("result...."+result);


String qid=(String)ADFUtils.evaluateEL("#{sessionScope.qotSession}");
quoteid=Integer.parseInt(qid);
fileName=file.getFilename();
fileContent=file.getContentType();
byte[] buff = result.getBytes();
// blobContent=new SerialBlob(buff);
try{
blobContent.setBytes(1,buff);
}
catch(Exception e){
e.printStackTrace();
}//this.blobContent=result;
String description=this.fileDescription.getValue().toString();



try{
final Context context= getInitialContext();
SessionEJB sessionEJB= (SessionEJB)context.lookup("SessionEJB#view.SessionEJB");
sessionEJB.addAttachment(quoteid,fileName,description,blobContent,fileContent);
}catch(Exception e){
e.printStackTrace();
}

}else{
System.out.println("not text/plain mimeType");
System.out.println("not text/plain .. result...."+result);
}
System.out.println("successfully uploaded the file..."+fileName);
System.out.println("size of the file is ..."+file.getLength()+"bytes");
FacesContext context = FacesContext.getCurrentInstance();

FacesMessage message = new FacesMessage(
"Successfully uploaded file " + file.getFilename() +
" (" + file.getLength() + " bytes)");
context.addMessage(actionEvent.getComponent().getClientId(context), message);

}
else
System.out.println("no content in file");


}

till byte array am getting the value but in blobContent am getting null value.
Please look into it as it is urgent.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2012
Added on Jul 12 2012
0 comments
2,120 views