java - 如何使用 af 上传文件 :inputFile in Oracle ADF

标签 java file-upload oracle-adf jdeveloper

谁能告诉我如何使用 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/

相关文章:

java - 在Java中使用模式、匹配器

实现 Hibernate Search 时出现 Java 链接错误

使用来自reflect.Method的通用签名信息生成Java字节码

php - 使用PHP上传文件时出错

java - ADF - 自定义 <af :commandButton> width the property styleClass

Oracle ADF 12 项目结构

java - 两个类之间如何发生碰撞和 react ?

html - 通过网页上传大文件

php - 使用 Zend Framework 忽略文件上传的空实例?文件因此无法上传

java - jsp 页面上的 URI 面临问题