java - how to uploaded and read text file in jsf and primeface? -
i need upload , read text file primeface , jsf. question when uploaded text file, stored? here .xhtml , java class:
<p:messages showdetail="true" /> <p:fileupload value="#{send.file }" mode="simple" /> </h:form> <p:commandbutton actionlistener="#{send.upload}" value="send" ajax="false" /> //send.java private uploadedfile file; public void upload() { if(file != null) { facesmessage msg = new facesmessage("succesful", file.getfilename() + " uploaded."); facescontext.getcurrentinstance().addmessage(null, msg); }
i found example read file:
import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception; public class bufferedreaderexample { public static void main(string[] args) { try (bufferedreader br = new bufferedreader(new filereader("c:\\testing.txt"))) { string scurrentline; while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); } } catch (ioexception e) { e.printstacktrace(); } }
}
my question in example "c:\testing.txt" given path? address must give read uploaded file? 'm sorry if confused you, thank you.
when uploaded text file, stored?
this none of business , should not interested in inside jsf backing bean code. it's stored (partial) in memory and/or (partial) in server's temporary storage location wiped/cleaned @ intervals. it's absolutely not intented permanent storage location. should in action/listener method read uploaded file content , store in permanent storage location choice.
e.g.
private static final file location = new file("/path/to/all/uploads"); public void upload() throws ioexception { if (file != null) { string prefix = filenameutils.getbasename(file.getname()); string suffix = filenameutils.getextension(file.getname()); file save = file.createtempfile(prefix + "-", "." + suffix, location); files.write(save.topath(), file.getcontents()); // add success message here. } }
note filenameutils
part of apache commons io should have installed it's required dependency of <p:fileupload>
. note file#createtempfile()
in above example not generate temp file, it's been used generate unique filename. otherwise, when else coincidentally uploads file same name existing one, overwritten. note files#write()
part of java 7. if you're still on java 6 or older, grab apache commons io ioutils
instead.
Comments
Post a Comment