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.
Bookmarks