Get file content of a file uploaded using rich:fileUpload
So I have an xhtml page that contains the following code:
<rich:fileUpload
id="uploadReportTemplate"
addControlLabel="Add XSLT"
fileUploadListener="#{manageFeedTypeAction.fileUploadListener}"
uploadData="#{manageFeedTypeAction.data}"
listWidth="63px" listHeight="0px" maxFilesQuantity="1"
immediateUpload="true" acceptedTypes="xsl,xslt"
allowFlash="false"
status="eventQueueFileUpload"
ontyperejected="javascript:Richfaces.showModalPanel('wrongSelectionModal');this.disabled=false">
<a4j:support event="onuploadcomplete" reRender="fileUploadPanel"/>
</rich:fileUpload>
I am only allowing the user to upload one file.开发者_运维技巧 Once this file has been uploaded, I wish to check if the file contains a certain keyword. How can I check whether the file contains that keyword? Please help.
I was able to solve this by adding the following code to the fileUploadListener method being called at fileUploadListener="#{manageFeedTypeAction.fileUploadListener}"
above:
public void fileUploadListener(UploadEvent event) {
UploadItem item = event.getUploadItem();
if(item == null || item.getData() == null) {
LOG.error("Uploaded item is null");
} else {
String value = new String(item.getData());
if(value.toLowerCase().contains("String")) {
LOG.error("Cannot contain 'String'");
} else {
setData(item.getData());
setFileName(item.getFileName());
}
}
}
精彩评论