Can anyone please tell me how to upload file to server using af:inputFile in Oracel ADF. I searched about this and found we can use the following
<af:form usesUpload="true">
<af:inputFile columns="10"
valueChangeListener="#{backing.fileUploaded}"/>
</af:form>
using the above code I can set a method that executes when some choose some file in the form. So now I need to know in fileUploaded method what should be the java code to upload the given file to the server.
Please help me. How I can achieve this.
Thanks in advance.
As you have already created value change listener in managed bean then use this code -
/**Method to Upload File ,called on ValueChangeEvent of inputFile
* @param vce
*/
public void uploadFileVCE(ValueChangeEvent vce) {
if (vce.getNewValue() != null) {
//Get File Object from VC Event
UploadedFile fileVal = (UploadedFile) vce.getNewValue();
}
}
and this is the method to upload file on server (You have to provide absolute path)
/**Method to upload file to actual path on Server*/
private String uploadFile(UploadedFile file) {
UploadedFile myfile = file;
String path = null;
if (myfile == null) {
} else {
// All uploaded files will be stored in below path
path = "D://FileStore//" + myfile.getFilename();
InputStream inputStream = null;
try {
FileOutputStream out = new FileOutputStream(path);
inputStream = myfile.getInputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();
} catch (Exception ex) {
// handle exception
ex.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
//Returns the path where file is stored
return path;
}
Check this thread on OTN Forum https://community.oracle.com/message/13135474#13135474
Here you can read full implementation and download sample application to test http://www.awasthiashish.com/2014/08/uploading-and-downloading-files-from.html
Full disclosure on the last link above and its contents: I wrote it and it is my TLD.