JavaRanch Home    
 
This page:         last edited 01 March 2013         What's Changed?         Edit

File Upload   

File Uploads in Java

Using HTTP

On the server

If you are using Servlets 3.0 or later, see the Servlet Specification for information on uploading files.

Servlet 2.x and JSP do not make any special provisions for multipart requests (file uploads), so when using Servlet 2.x and JSP, developers either have to depend on third party libraries or make their own framework.

Fabricating your own framework is incredibly difficult (multi-part requests are not trivial to parse) and would be re-inventing the wheel. Fortunately, there are several very good open source frameworks out there; with clear instructions and code examples.

The two most popular are Jakarta Commons FileUpload and com.oreilly.servlet

Note that request.getParameter() and its related methods do not work with multi-part requests, and will always return null when dealing with multipart form data.

On the client

If you want to upload files in conjunction with Commons HttpClient, this introductory article tells how to use FileUpload in conjunction with the Commons HttpClient package.


Using FTP

It is possible to do basic FTP operations using J2SE:


URL url = new URL("ftp://user:password@somewhere.com/pub/somefile.xxx");
URLConnection conn = url.openConnection();
 
// to upload, open an OutputStream, to download, open an InputStream:
OutputStream os = conn.getOutputStream();
// read/write your file to/from the Stream

To obtain a directory listing, simply create the URL with the directory you wish to index.

The Jakarta Commons Net package includes an FTP client which supports the full range of FTP commands.


Using SMB

The Samba project has a Java client library that can access Windows shares and Samba servers. Its API mimics the java.io.File class, and is thus easy to use if you are familiar with File.


Using WebDAV

Tomcat ships with a WebDAV server component. The client side could be one of numerous available libraries, or the file managers of Mac OS X or Windows.


Back to the JspFaq ServletsFaq AppletsFaq


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton