谁能告诉我如何使用 Oracel ADF 中的 af:inputFile 将文件上传到服务器。我搜索了这个,发现我们可以使用以下
<af:form usesUpload="true">
<af:inputFile columns="10"
valueChangeListener="#{backing.fileUploaded}"/>
</af:form>
使用上面的代码,我可以设置一个在某些人选择表单中的某个文件时执行的方法。所以现在我需要知道 fileUploaded 方法中将给定文件上传到服务器的 java 代码应该是什么。
请帮助我。我如何才能实现这一目标。
提前致谢。
最佳答案
由于您已经在托管 bean 中创建了值更改监听器,因此请使用此代码 -
/**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();
}
}
这是在服务器上上传文件的方法(您必须提供绝对路径)
/**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;
}
在 OTN 论坛上查看此帖子 https://community.oracle.com/message/13135474#13135474
在这里您可以阅读完整的实现并下载示例应用程序进行测试 http://www.awasthiashish.com/2014/08/uploading-and-downloading-files-from.html
全面披露上面最后一个链接及其内容:我编写了它,它是我的 TLD。
关于java - 如何使用 af 上传文件 :inputFile in Oracle ADF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30888621/