12.09.2007

ADF Upload File


Jdeveloper web uygulaması üzerinden dosya upload etmek için;

Resim yükleme sayfasından bir upload pencersi açıp buradan seçilen resmi yükle dediğimizde sisteme kaydediyoruz.... ;



action="dialog:resimUpload"
useWindow="true"
windowHeight="200"
windowWidth="200"
partialSubmit="true"
returnListener="#{backing_okul_ogrenciEdit.onReturnFromSelectDepartmentDialog}">


poup ile açılan sayfa kodu:


id="myUploadFile"
valueChangeListener="#{backing_okul_ogrenciResimUpload.fileUploaded}"/>


id="cellFormat1">

binding="#{backing_okul_ogrenciResimUpload.btnResimYukle}"
id="btnResimYukle"
action="#{backing_okul_ogrenciResimUpload.btnResimYukle_action}">



Yükleme işlemi için bean kodu:

public String btnResimYukle_action() {
System.out.println("cmdUpload_action" + this.getMyUploadFile().getValue());
if (this.getMyUploadFile().getValue() == null){
System.out.println("cmdUpload_action: value is null ");
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "*** No File Specified ***", null);
context.addMessage(this.getMyUploadFile().getId(), message);
}
return null;
}

public void fileUploaded(ValueChangeEvent event) {
System.out.println("fileUploaded");
InputStream in;
FileOutputStream out;
// Set fileUPloadLoc to "MY.FILE_UPLOADS_DIR" context init parameter
//selectedMobile.getValue()

String fileUploadLoc = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("AKKOR.IMAGE_UPLOADS_DIR");
String fileFTPCopy = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("AKKOR.IMAGE_FTP_OUT_DIR");

fileUploadLoc = fileUploadLoc + "/ogrenci_resim/" + this.getOkulId().getValue()+"/"+this.getBolumId().getValue()+"/"+ this.getSinifId().getValue();

System.out.println(fileUploadLoc+"/"+this.getOgrenciNo().getValue()+"/"+ this.getSinifId().getValue()+"/"+ this.getBolumId().getValue()+"/"+ this.getOkulId().getValue());
if (fileUploadLoc == null) {
// Backup value if context init parameter not set.
fileUploadLoc = "/temp/myUploadedFiles";
}

//Integer svrId = (Integer)UploadUtils.getManagedBeanValue("userState.currentSvrId");
//fileUploadLoc += "/my_" + svrId + "_uploadedfiles";

// Create upload directory if it does not exists.
boolean exists = (new File(fileUploadLoc)).exists();

if (!exists) {
(new File(fileUploadLoc)).mkdirs();
}
UploadedFile file = (UploadedFile)event.getNewValue();

if(!file.getContentType().equals("image/pjpeg")){
// resim dosyas? de?il o zaman hata ver...
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("Dosya tipi uygun değil.. " + file.getFilename() + " (" + file.getLength() + " bytes)");
context.addMessage(event.getComponent().getClientId(context),message);
return;
}
if (file != null && file.getLength()>0) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage("*** successful upload *** " + file.getFilename() + " (" + file.getLength() + " bytes)"); context.addMessage(event.getComponent().getClientId(context),message);
try {

//new FileOutputStream(fileUploadLoc + "/" + file.getFilename());
//out = new FileOutputStream(fileUploadLoc + "/" +selectedDriver.getValue()+".jpg");

System.out.println("Öğrenci resim...:"+fileUploadLoc +"/"+ this.getOgrenciNo().getValue() +".jpg");
out = new FileOutputStream(fileUploadLoc +"/"+ this.getOgrenciNo().getValue() +".jpg");


in = file.getInputStream();
for (int bytes = 0; bytes <>
out.write(in.read());
}
in.close();
out.close();

} catch (IOException e) {
e.printStackTrace();
}
// dosya ftpde gidenler bolümüne atılacak
if((new File(fileFTPCopy)).exists()){
(new File(fileFTPCopy)).mkdirs();
}
CopyFile cp = new CopyFile();
cp.CopyImageFile(new File(fileUploadLoc +"/"+ this.getOgrenciNo().getValue() +".jpg"), new File(fileFTPCopy+"/"+this.getOgrenciNo().getValue() +".jpg"));

} else {
// need to check for null value here as otherwise closing
// the dialog after a failed upload attempt will lead to
// a nullpointer exception
String filename = file != null ? file.getFilename() : null;
String byteLength = file !=null ? "" + file.getLength() : "0";
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,"*** Error Uploading ***" +
filename + " (" + byteLength + " bytes)", null);
context.addMessage(event.getComponent().getClientId(context),message);
}
}

Resim upload penceresini çağıran ana sayfa
backing_okul_ogrenciEdit.onReturnFromSelectDepartmentDialog metodu ile upload penceresinden dönen değeri dinleyecektir.

backing_okul_ogrenciEdit.onReturnFromSelectDepartmentDialog kodu:

public void onReturnFromSelectDepartmentDialog(ReturnEvent returnEvent) {

String keyString =(returnEvent.getReturnValue()).toString();
String gelenler[] = new String[5];
gelenler = keyString.split("/");
String imageName = "/ogrenci_resim/"+gelenler[4]+"/"+gelenler[3]+"/"+gelenler[2]+"/"+gelenler[0]+".jpg";
boolean sonuc = ADFUtils.getAkkorAppModuleInterface().updateOgrenciImage(gelenler[1], imageName);
refreshCurrentPage();
}

protected void refreshCurrentPage() {
FacesContext context = FacesContext.getCurrentInstance();
String currentView = context.getViewRoot().getViewId();
ViewHandler vh = context.getApplication().getViewHandler();
UIViewRoot x = vh.createView(context, currentView);
x.setViewId(currentView);
context.setViewRoot(x);
}