Download presentation
Presentation is loading. Please wait.
Published byJeffrey Watson Modified over 9 years ago
1
CSC 2720 Building Web Applications File Upload using Apache Commons – FileUpload
2
HTTP File Upload (RFC 1867)RFC 1867 Set attribute METHOD="POST" Set attribute ENCTYPE="multipart/form-data" Use INPUT element with TYPE="file" to create a file upload control (one control per file) <form action="MyServlet" method="post" enctype="multipart/form-data"> Currently a "Request for Comments" awaits to be made into an Internet Standard
3
POST register.jsp HTTP/1.1 Host: hi/iq User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8, video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Language: en-us, en;q=0.50 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------29772313742745 Content-Length: 452 -----------------------------29772313742745 Content-Disposition: form-data; name="name" J.Doe -----------------------------29772313742745 Content-Disposition: form-data; name="email" abuse@spamcop.com -----------------------------29772313742745 Content-Disposition: form-data; name="file-upload"; filename="test.txt" Content-Type: text/plain test data with some high ascii: ¿Como estás? -----------------------------29772313742745-- An example of data sent via POST method with enctype="multipart/form-data" (Copied from http://www.devx.com/Java/Article/17679/0/page/2)http://www.devx.com/Java/Article/17679/0/page/2
4
Decoding Multipart POST Request Java Servlet API does not provide any high level method to retrieve form data from a Multipart POST Request request.getParameter(…) don't work on multipart request. Need to use request.getInputStream() to access the body of a multipart request and decode the data accordingly, or use third party library e.g., Apache Commons – FileUpload 1.2.x http://commons.apache.org/fileupload/
5
Using FileUpload 1.2.x in NetBeans Download: Apache Commons – FileUpload (http://commons.apache.org/fileupload) Apache Commons – IO (http://commons.apache.org/io) Unzip the files you download. In the "Projects" view, right-click the "Libraries" folder of your project and add these JAR files one by one. commons-fileupload-1.2.x.jar commons-io-1.4.jar
6
Checking if request is Multipart If request is not multipart, we can process the request normally (using request.getParameter()) Otherwise we need to use the classes in the FileUpload library to retrieve the parameters and the uploaded file. We shall not use both approaches at the same time. // import org.apache.commons.fileupload.servlet.*; boolean isMultipart = ServletFileUpload.isMultipartContent(request);
7
Simple Usage Scenerio Uploaded items should be retained in memory as long as they are reasonably small. Larger items should be written to a temporary file on disk. Very large upload requests should not be permitted. The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable.
8
Simple Usage Scenerio items is a list of objects that implements the FileItem interface. Need to import the proper packages. // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request and obtain items in the multipart // request (including parameters) List items = upload.parseRequest(request);
9
Exercising more controls // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold(yourMaxMemorySize); factory.setRepository(yourTempDirectory); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(yourMaxRequestSize); // Parse the request and obtain items in the multipart // request (including parameters) List items = upload.parseRequest(request);
10
Processing the uploaded items // Process the uploaded items for (Object obj : items) { FileItem item = (FileItem) obj.next(); if (item.isFormField()) { // Process a regular form field String name = item.getFieldName(); String value = item.getString();... } else { // Process uploaded file String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize();... }
11
Saving uploaded file to disk if (writeToFile) { // Saving uploaded file to disk File uploadedFile = new File(...); item.write(uploadedFile); } else { // Process the uploaded file as an input stream InputStream uploadedStream = item.getInputStream();... uploadedStream.close(); }
12
Process Multipart Request as Stream (Save Space) // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); if (item.isFormField()) { // Regular form field System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected."); } else { // An uploaded file System.out.println("File field " + name + " with file name " + item.getName() + " detected."); // Process the input stream... }
13
References http://commons.apache.org/fileupload/ http://commons.apache.org/fileupload/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.