任何人都可以告诉我如何使用af:inputFile在Oracel ADF中将文件上传到服务器。我搜索了这个,发现我们可以使用以下
<af:form usesUpload="true">
<af:inputFile columns="10"
valueChangeListener="#{backing.fileUploaded}"/>
</af:form>
使用上面的代码我可以设置一个方法,当一些人在表单中选择一些文件时执行。所以现在我需要知道在fileUploaded方法中应该将给定文件上传到服务器的java代码。
请帮帮我。我怎么能做到这一点。
提前致谢。
答案 0 :(得分:2)
由于您已在托管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
完整披露上述最后一个链接及其内容:我写了它,这是我的顶级域名。
答案 1 :(得分:1)
您的inputFile应该类似于以下内容,其中gciuiCheckin是对辅助bean的引用。在这种情况下,inputFile控件在jsff中包含jspff作为jspx中的一个区域,其中usesUpload =&#34; true&#34;,但如果你将控件直接放在jspx中,这将是相似的,主要是你需要将控件值绑定到UploadedFile类型的支持bean变量:
<af:inputFile label=" " id="ifDoc" columns="50"
value="#{pageFlowScope.gciuiCheckin.filesToUpload}"
maximumFiles="#{pageFlowScope.gciuiCheckin.maxFilesCanBeUploaded}"
autoHeightRows="0" rows="5" uploadType="auto"/>
然后你也应该有一个commandButton来在用户选择文件后调用bean方法(每个文件通常在用户选择或拖动每个文件时上传到服务器):
<af:commandButton text="Commit File(s)" id="cbUpload"
partialSubmit="true" action="#{pageFlowScope.gciuiCheckin.saveUploadedFilesAction}"/>
您需要在辅助bean中进行此导入:
import org.apache.myfaces.trinidad.model.UploadedFile;
在辅助bean中,创建一个带有访问器的List来保存上传的文件:
private List<UploadedFile> filesToUpload;
在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...
}
...
希望这有帮助。