Results 1 to 6 of 6

Thread: How to do FileUpload via HTTP

  1. #1
    Join Date
    Jul 2010
    Posts
    93

    How to do FileUpload via HTTP

    Commons FileUpload is a small library that really cares only about the range of file uploads via the World Wide Web. Because the FileUpload library focuses on such a narrowly defined area, its use is easy to understand. To that extent at this point as little as a few examples and comments. First of all, to see how a FileUpload via HTTP goes - the features here are the ones who ultimately justify all its own library. During a normal POST request parameters directly from a key-value pairs in the body of requests can be found, file uploads are handled differently. This concerns both the HTML code, and the transport of the file and the other along with the file sent via HTTP parameters and the evaluation in a servlet or JSPs.

    First, we consider the changes in HTML code. Here you have the <form> element of the HTML page of the form as shown in the following code fragment encoding the set:
    Code:
    <form method="POST" enctype="multipart/form-data" action="/UploadServlet"> 
    <label for="uploadFile"> File </ label> 
    <input type="file" size="50" name="uploadedFile" class="fileSelector"/> 
    </ Form>
    If you forget the HTML code to set the encoding using the attribute "enctype", so not the file contents, but only the file name will be transferred.

    Also note that files according to the HTTP specification, only POST can be uploaded to the server. If you use a dedicated upload servlet, it is recommended that the doGet() method like this to write:
    Code:
    public void doGet (HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException ( 
    
    response.sendError (HttpServletResponse.SC_METHOD_NOT_ALLOWED, 
    "GET is not allowed for uploads"); 
    )
    Thus, any attempt to upload a file via GET, answered with a reasonable error. Regardless of the response from the server GET works as an incorrectly set attribute "enctype". That means instead of the files are transferred, only the file name. What it does mean the attribute enctype to multipart/form-data set? The effect of this can be seen best by a concrete request.

  2. #2
    Join Date
    Jul 2010
    Posts
    93

    Re: How to do FileUpload via HTTP

    First, a text field has been transferred - the references to the content type and file name are missing accordingly. In the following two parts of the POST data, it is obviously files. There is the header "filename", in which the file name stands, and a header "Content-Type", which contains information about file types. Clearly we also recognize the difference between binary data and transferred data from text files. This transport mechanism is clearly different from the normal mechanism. This has a corresponding impact on the reading of request parameters. One makes use normally to the method getParameter(String) of the Request object the more no - you get as a return value of null returns, regardless of whether it appropriately named form element. In the usual way to get in via multipart/form-data transmitted Fomular neither the contents of the uploaded file or the other request parameters approached.

    Dependencies :

    Commons FileUpload is a small, very specialized library. Accordingly, Commons FileUpload only by the Library Commons-IO dependent.

    Use of Commons FileUpload :

    Commons HttpClient is also similar in FileUpload two central classes / interfaces: org.apache.commons.fileupload.FileItem and org.apache.commons.FileUpload. We first show a small example of usage:
    Code:
    org.jsptutorial.examples.servlets package; 
    
    Import java.io.IOException; 
    import java.util .*; 
    import javax.servlet .*; 
    import javax.servlet.http .*; 
    org.apache.commons.fileupload import .*; 
    org.apache.commons.fileupload.disk import .*; 
    import org.apache.commons.fileupload.servlet .*; 
    
    
    public class extends HttpServlet (SimpleFileUploadServlet 
    
    protected void doPost (HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException ( 
    
    // Step 1: create DiskFileItemFactory 
    DiskFileItemFactory DiskFileItemFactory factory = new (); 
    
    // Step2: create ServletFileUpload factory and pass to constructor 
    ServletFileUpload upload = new ServletFileUpload (factory); 
    try ( 
    / Optional but highly recommendable step: 
    // Verify if we really have to upload 
    RequestContext ctx = new ServletRequestContext (request); 
    if (ServletFileUpload.isMultipartContent (ctx)) ( 
    // Step3: parse request 
    List list = <DiskFileItem> upload.parseRequest (request); 
    // Step 4: do s.th. meaningful with each part 
    for (DiskFileItem fileItem: list) ( 
    String form = fileItem.getFieldName Field (); 
    String fileName = fileItem.getName (); 
    // ... 
    ) 
    ) 
    ) 
    catch (FileUploadException e) ( 
    response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
    "Fileupload reason failed;:" + e.toString ()); 
    ) 
    / / Some response for the client 
    / / ... 
    ) 
    
    )

  3. #3
    Join Date
    Jul 2010
    Posts
    93

    Re: How to do FileUpload via HTTP

    As seen in the description of the structure of upload requests in the previous section, contain multi-part questions on their behalf in accordance with several parts, each dealing with a parameter of a form. The structure is similar to the mail format for emails with attachments, known as MIME format. By the header "Content-Type" specified delimiters ("boundary") separated from each other and contain the header information to be included on the normal request header also provide additional information to the respective parts (for example, uploads the name of the form element, the file name and the data format). The individual parts of a multipart requests are Commons FileUpload represented by objects of type org.apache.commons.fileupload.FileItem . Even normal form elements that were sent along with an upload, are represented as FileItems, the name is thus chosen to be somewhat unhappy. Are generated using a suitable FileItems FileItemFactory . This is also an interface from the Package org.apache.commons.fileupload . This factory needs the FileUpload object before it parses the request, either by delivery to the constructor or by calling the method setFileItemFactory(). The really interesting work done in the subclasses of org.apache.commons.fileupload.FileUploadBase : for Servlet-based uploads, this is org.apache.commons.fileupload.servlet.ServletFileUpload for portlet-based uploads org.apache.commons.fileupload.portlet.PortletFileUpload.

    The main method is already visible in the above example. It is the method parseRequest, depending on your environment an javax.servlet.http.HttpServletRequest or javax.portlet.ActionRequest is given. The return value of this method is a list of FileItem objects. In the respective FileItems you can now have the method get() the result in the form of a byte array, using the method getString() as string or with the method getInputStream() InputStream fetch as. The library itself takes only an implementation of the FileItem - and FileItemFactory interfaces with: DiskFileItem and DiskFileItemFactory from the package org.apache.commons.fileupload.disk.

    Also in the name corresponds to the not quite, what one would expect. Unlike the name, not all DiskFileItems be stored directly on the plate, but at first only those that exceed a given size. The default size, elements on the disk are cached from the is 10 kilobytes can, however, by calling the method setSizeThreshold(int) of DiskFileItemFactory be changed. Similarly, the DiskFileItemFactory for the temporary files folder of the award will be. If this value is not set, the system property used for temporary files ("java.io.tmpdir").

  4. #4
    Join Date
    Mar 2008
    Posts
    258

    Re: How to do FileUpload via HTTP

    FileUpload block of ServletContainers thread for a long time. Here the maximum number of allowable threads simultaneously editable is limited. In case that the amount of data uploadable at its default setting, ie one not limited in the amount of data, it can have relatively few unexpectedly large upload streams lay the lame server. It also has a general guideline for a maximum upload size do not specify one. This is the available resources and above all of the planned application dependent. What is certain: You should have a clearly defined border.

  5. #5
    Join Date
    Apr 2008
    Posts
    193

    Re: How to do FileUpload via HTTP

    The threads of the thread pool of a servlet container without special precautions are all requests equally available, regardless of whether they are uploads or standard requests. Since uploads are time consuming and always a block of threads for a relatively long time, the rest of the application are correspondingly fewer threads. This may need the settings of the thread pool (to be in Tomcat, this is in the connector element in the file "server.xml configured, with GlassFish in the" request-processing element in the file "domain.xml") adapted. An automatic monitoring of the thread pool has also participated in - in the starting phase of a new application, monitoring of the pools to fine-tune the configuration is a necessity. As an additional point should be - in the Commons HttpClient said - even with Commons FileUpload note similar to that one except for small uploads the result never get() array should be read as a byte. Instead, it is recommended that the data is always in the form of an input stream, the one with getInputStream() continues to process can bring. In this way several times, unnecessary copying of data is avoided.

  6. #6
    Join Date
    Dec 2008
    Posts
    183

    Re: How to do FileUpload via HTTP

    In the section "Use of Commons FileUpload," earlier it was mentioned that some interfaces FileItemFactory and FileItem each case an implementation is only to save the FileItems to the disk control together, where each multi-part components exceed a certain size. This may not always be desirable. It is conceivable that one wants to store the incoming uploads the same in a database or pass through the network. In such cases, makes no sense, the temporary store. As a solution, one could write implementations, the data only in memory keep. Example is an implementation that I have recently made before, in which a FileItem implementation with listener support to bear came to Ajax-based file upload progress bar to show the can.

Similar Threads

  1. How to fix HTTP error 307 Temporary Redirect (since HTTP/1.1)?
    By Charu Sharma in forum Networking & Security
    Replies: 6
    Last Post: 05-04-2012, 01:14 AM
  2. HTTP: 12029 no HTTP access for IE
    By Captainlumpy in forum Windows XP Support
    Replies: 5
    Last Post: 29-07-2010, 06:43 AM
  3. Manage Fileupload with Javascript
    By Zebaril in forum Software Development
    Replies: 3
    Last Post: 07-11-2009, 09:27 PM
  4. HTTP 400 Bad Request
    By adyf in forum Vista Help
    Replies: 2
    Last Post: 04-11-2008, 02:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,701,355.38896 seconds with 17 queries