Tuesday, 15 March 2011

get directory of uploaded file java -



get directory of uploaded file java -

i'm trying upload excel file , read it. can read file when give absulute path, doesn't want work when upload it. i've tried diffrent things can't find solution. i'm not using jsp , servlets, html/controller/service

html page

<h:form enctype="multipart/form-data"> <h:panelgrid> <h:outputlabel value="upload file: " style="font-weight:bold" /> <h:inputfile value="#{uploadcontroller.path}" /> <h:commandbutton value="excel lezen en opslaan" action="# {uploadcontroller.excelopslaan}"/> </h:panelgrid> </h:form>

controller

private string path; public string getpath() { homecoming path; } public void setpath(string path) { this.path = path; } public void excelopslaan(){ puntenservice.getexcel(path); }

service

public list<string> getexcel(string path);

service impl

private list<string> list = new arraylist<string>(); @override public list<string> getexcel(string path){ fileinputstream file = null; opcpackage pkg = null; seek { file = new fileinputstream(path); // create new opc bundle obtain workbook pkg = opcpackage.open(file); //create workbook instance holding reference .xlsx file xssfworkbook workbook = new xssfworkbook(pkg); //get first/desired sheet workbook xssfsheet sheet = workbook.getsheetat(0); //iterate through each rows 1 1 iterator<row> rowiterator = sheet.iterator(); while (rowiterator.hasnext()) { row row = rowiterator.next(); //for each row, iterate through columns if (row.getrownum() <= 5) { continue;// skip read first 7 row of file } iterator<cell> celliterator = row.celliterator(); while (celliterator.hasnext()) { cell cell = celliterator.next(); switch (cell.getcelltype()) { case cell.cell_type_string: list.add(cell.getstringcellvalue()); break; } } //system.out.println(""); } file.close(); } grab (exception e) { e.printstacktrace(); } homecoming list; }

i not going deep program, seek adapt java7+ standard (e.g. store path path, not string), maybe helps some..

some examples

list<string> readsmalltextfile(string afilename) throws ioexception { path path = paths.get(afilename); homecoming files.readalllines(path, encoding); } //for larger files void readlargertextfile(string afilename) throws ioexception { path path = paths.get(afilename); seek (scanner scanner = new scanner(path, encoding.name())){ while (scanner.hasnextline()){ //process each line in way log(scanner.nextline()); } } } void readlargertextfilealternate(string afilename) throws ioexception { path path = paths.get(afilename); seek (bufferedreader reader = files.newbufferedreader(path, encoding)){ string line = null; while ((line = reader.readline()) != null) { //process each line in way log(line); } } }

source http://www.javapractices.com/topic/topicaction.do?id=42

java file upload path controller

No comments:

Post a Comment