java 如何在 Oracle ADF 中使用 af:inputFile 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30888621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:47:32  来源:igfitidea点击:

How to upload a file using af:inputFile in Oracle ADF

javafile-uploadoracle-adfjdeveloper

提问by Abdul

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

谁能告诉我如何在 Oracel ADF 中使用 af:inputFile 将文件上传到服务器。我搜索了这个,发现我们可以使用以下内容

<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.

使用上面的代码,我可以设置一个方法,该方法在某些人选择表单中的某个文件时执行。所以现在我需要在 fileUploaded 方法中知道将给定文件上传到服务器的 java 代码应该是什么。

Please help me. How I can achieve this.

请帮我。我如何才能做到这一点。

Thanks in advance.

提前致谢。

采纳答案by Ashish Awasthi

As you have already created value change listener in managed bean then use this code -

由于您已经在托管 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();
    }
}

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

在 OTN 论坛上查看此主题 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

在这里你可以阅读完整的实现并下载示例应用程序来测试 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.

上面最后一个链接及其内容的完整披露:我写的,它是我的顶级域名。

回答by 32U

Your inputFile should look something like the following where gciuiCheckin is a reference to the backing bean. In this case the inputFile control is in a jsff contained as a region inside a jspx with usesUpload="true", but this would be similar if you are putting your controls directly in the jspx, the main thing is you need to bind the control value to a backing bean variable of type UploadedFile:

您的 inputFile 应该类似于以下内容,其中 gciuiCheckin 是对支持 bean 的引用。在这种情况下,inputFile 控件位于一个 jsff 中,作为一个区域包含在一个带有 usesUpload="true" 的 jspx 中,但是如果您将控件直接放在 jspx 中,这将是类似的,主要是您需要绑定控件类型为 UploadedFile 的支持 bean 变量的值:

<af:inputFile label=" " id="ifDoc" columns="50" 
  value="#{pageFlowScope.gciuiCheckin.filesToUpload}" 
  maximumFiles="#{pageFlowScope.gciuiCheckin.maxFilesCanBeUploaded}"
  autoHeightRows="0" rows="5" uploadType="auto"/>

Then you also should have a commandButton to call a bean method once user has selected the file (each file typically is uploaded to server as the user selects or drag drops each one):

然后,您还应该有一个 commandButton 来在用户选择文件后调用 bean 方法(每个文件通常在用户选择或拖放每个文件时上传到服务器):

<af:commandButton text="Commit File(s)" id="cbUpload" 
  partialSubmit="true" action="#{pageFlowScope.gciuiCheckin.saveUploadedFilesAction}"/>

You will need this import in the backing bean:

您将需要在支持 bean 中进行此导入:

import org.apache.myfaces.trinidad.model.UploadedFile;

In the backing bean, create a List with accessors to hold the uploaded files:

在支持 bean 中,创建一个带有访问器的 List 来保存上传的文件:

private List<UploadedFile> filesToUpload;

In the method called by the commandButton, you will do something like:

在 commandButton 调用的方法中,您将执行以下操作:

public String saveUploadedFilesAction() {        
        List<UploadedFile> files = this.getFilesToUpload();

        if (files == null || files.size() == 0) {            
            displayMessageToUser(FacesMessage.SEVERITY_WARN, checkinErrorMessage);
            return null;
        }

        //iterate each file and check size, extension, etc...        
        for (int i = 0; i < files.size(); i++) {                
            UploadedFile currFile = files.get(i);
            //now do something with the file...                
        }
        ...

Hope this helps.

希望这可以帮助。