2

I'm working on a multiple file upload solution for a proof-of-concept web application. I'm using a java servlet to handle an AJAX file upload. My question is how does java handle uploading files from an HTML form? If someone could explain how a basic HTML file upload is processed then I could probably port this to my solution.

Quick tangent: I'm a web developer with a background in C# & PHP. I'm trying to hop on the Java bandwagon now that I've taken a new position where mycompany believes Java is the holy grail of all programming languages. I feel like I'm missing something here... I definitely like the feel of the Java language and how easy it is to run applications. But its seems infinitely difficult to use as a web programming language.

Thanks in advance.

John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • 1
    There's an Apache project that's a library to do the MIME unpacking etc. It's an inherently messy problem for *any* server-side environment, but out of the Oracle box Java really doesn't do you any favors. (Really, Java as a web application language really only makes sense once you've picked one of the hundreds of frameworks to build on.) – Pointy Jan 28 '11 at 21:16
  • [Here](http://commons.apache.org/fileupload/) is the project I was thinking about - personally I use it via the Stripes web framework, which adds its own layer of convenience over that. – Pointy Jan 28 '11 at 21:20
  • Thanks Pointy. That's the project I was looking at a couple minutes ago too... being new to the Java scene, I wasn't sure what route to take yet. I'll check it out further. – John Strickler Jan 28 '11 at 21:25
  • Answer: http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 – BalusC Jan 29 '11 at 00:02

2 Answers2

3

You can use Commons FileUpload library:

http://commons.apache.org/fileupload/

Here is simple example of it's usage:

// 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
List /* FileItem */ items = upload.parseRequest(request);

I taken this example from here:

http://commons.apache.org/fileupload/using.html

tenshi
  • 26,268
  • 8
  • 76
  • 90
1

I just did it today. I followed this tutorial. It is specific for GWT, but author explained basics brilliantly.

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • Nice tutorial. The author is using the same library that pointy suggested above. I'm going to give it a try. Many thanks! – John Strickler Jan 28 '11 at 21:26
  • 1
    Yes, apache commons are realy 'common' to use :) You'll definitelly write less boilerplate with these libs. – Xorty Jan 28 '11 at 21:27