1) Get Started.

 

back to main index

 

Xload code is Java code that can be used inside a Servlet, JSP or similar location (web tier). Files that are uploaded and either written to disk or to memory (memory load) are termed 'file deployments' as they have originated from a file upload. There can be many file deployments for each file uploaded. Copies of file deployments also become file deployments in their own right.

 

To get started, refer to the installation instructions for Xload. You can upload a single file using a Servlet to some target directory by using the following code after creating the Servlet outlined below (the black font shows Xload specific code):

 

 

package mypack;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import com.gubutech.xload.*;

 

public class XloadServlet extends HttpServlet {

  

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

            

XloadManager xman = new XloadManager(request);

xman.target("file", "uploaded");

xman.upload();

}

...

...

}

 

 

where:

request - HttpServletRequest object.

uploaded - directory to upload files to (relative to the web application directory).

file - File parameter inside html (or other) form.

       

The html on the client side would look like:

 

 

  <html>

    <head>

<title>Xload</title>

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    </head>

    <body>

 <form action="/my_servlet" enctype="multipart/form-data" method="post">

file: <input type="file" name="file">

  </form>

</body>

  </html>

 

 

         

 

IMPORTANT:- File deployments only exist as file deployments for the life of the XloadManager object they were created from after which they revert to being arbitrary (or ordinary) files.

 

 

An XloadManager is created that wraps the Servlet request, a target is set for the location that the file is to be uploaded to and then the file is simply uploaded from the request to this location when the upload() method is called. Using this code a single file would be uploaded to the designated directory. Please note the encoding for the form (i.e. multipart/form-data) and the method (i.e. post) used; this must always be like this. By default Xload is set to decode text using the UTF-8 encoding but this can be altered manually using the XloadManager class if another page encoding is to be used (charset=UTF-8 is used in the example above, but other encodings can be used).

 

This example obviously can be expanded on greatly with the addition of more uploads, memory loading, partial uploads, upload failure management etc. but remains the backbone of any upload procedure when using Xload; including when used inside of architectures such as Struts etc.

 

Xload initially tries to write the file using its original (or 'remote') name and if a file name collision occurs (a file with the same name exists in the same directory) then the file name is changed by appending a (1) to the name and then an attempt is made to write using this name. If this fails the number is increased by one and the process repeated until a unique name is found. This is the default renaming policy, but Xload has another renaming policy available that renames each file deployment to an arbitrary unique number. Also, the programmer can implement their own file renaming policy if they so wish or use memory loading to control even finer the file naming process.

 

back to main index

top of page

 

 

 

 

© Gubutech(Xload) 2006  (v1.2)