开发者

Uploading a zip file to a jsp and extract the content in another jsp

I want to upload a zip file in a jsp page and the user is sent to another jsp page after he selects and uploads the file.

In this second page I want to

1.)extract the uploaded zip file.

2.)read some content from the extracted file(s) and allow the user to change/edit them

3.)save the user changes in the files.

4.)zip the files again and save it in another destination.

Please suggest me a rough outline 开发者_JAVA百科of a solution as I am stuck in the phase in which I get the uploaded file and re-direct the user to the second jsp.


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

  • How to upload files with JSP/Servlet?
  • Beginning and Intermediate-Level Servlet/JSP Tutorials
  • Compressing and Decompressing Data Using Java APIs
  • Lesson: Basic I/O
  • XSS prevention in Java


You might be getting the uploaded zip file in a servlet.

For your step 1, extract the zip file contents using http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/ZipFile.html

Procedure is here http://java.sun.com/developer/technicalArticles/Programming/compression/

Example is here http://www.devx.com/getHelpOn/10MinuteSolution/20447

Step 2 and 3 are simple

Step 4 of creating zip again is available in http://java.sun.com/developer/technicalArticles/Programming/compression/ (See Zip.java in that page)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜