I want to upload a zip file in a jsp page...
Use the following type of form:
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file">
<input type="submit">
</form>
...and the user is sent to another jsp page after he selects and uploads the file.
Hold on. You shouldn't be using JSP for business logic. Raw Java code doesn't belong in a JSP file, but in a real Java class. Let the form submit to a Servlet
, which in turn processes the file upload and finally forwards the request to a JSP page to display some result.
1) extract the uploaded zip file.
Use Apache Commons FileUpload to obtain the uploaded file. You can find code examples and tips&tricks(!) in respectively the User Guide and Frequently Asked Questions sections at their homepage.
You should end up with an InputStream
containing the uploaded file.
Once having that, then just use java.util.zip.ZipInputStream
API to read the uploaded file.
2) read some content from the extracted file(s) and allow the user to change/edit them
Get hold of the data in model objects (javabeans), store them in the request or session scope and use a JSP/EL to display model values in HTML input fields. E.g.
<input type="text" name="foo" value="${fn:escapeXml(bean.foo)}">
The JSTL fn:escapeXml
is just there to prevent XSS.
3) save the user changes in the files.
Let the form submit to a servlet, gather the new input, write it to some file.
4) zip the files again and save it in another destination.
Use ZipOutputStream
.
See also