java - How to customize the request from HTML to Servlet -
i building server using java's servlet , html forms.
i managed upload files: user reach html page, chose file he/she wants upload in his/her tree folder. file sent servlet i've written , downloaded on server (actually i'm running on localhost moment, server 'my documents' folder).
the next step make one: user (once logged, manage that) reaches html page, select file hosted server , download it.
to make it, have send 'download servlet' name of file. here questions:
- how list files in 'my documents' on html page.
- how send name of selected file servlet.
- how catch 'request' , make string of name out of it.
to precise these 2 lasts points, please have @ this:
list<fileitem> items = null; items = upload.parserequest(request); fileitem item = items.get(0); string filename = item.getname();
the block above catches name of folder in request. want do same thing if in request string (=catch string contained in request).
the file
api give need selecting files in directory.
list<file> files = arrays.aslist(new file("/your/directory").listfiles()); list<string> filenames = new linkedlist<>(); (file file : files) { filenames.add(someprefix + file.getname()); } request.setattribute("filenames", filenames);
i above because might not want give real path files, security reasons. once have list of file names in request attributes, can iterate on them in jsp.
<form ...> select file:<br /> <c:foreach items="${filenames}" var="filename"> <input type="radio" name="filename" value="${filename}"> </c:foreach> <input type="submit" name="submit" value="submit">submit </form>
now files each attached input element, translate request parameter. when form submitted, can access select file name doing
string filename = request.getparameter("filename");
you can append file name directory structure , go , find on file system.
Comments
Post a Comment