0

Hello I'm building a web application (online store) where an admin can upload a new product. One of the input type is a file (image of the product) I want the images of the products to be stored in a folder, and each product will have an image associated in the database table.

My question is, how does input type=file work? I still don't understand how when I submit the form a servlet will paste the image in the webpage folder, and how is the value (name of the image) going to be obtained to be stored in the database table?

For the other inputs i use "value" to get the info.

Thanks!

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
bb2
  • 27
  • 4
  • 7

3 Answers3

2

In java, its hard to do handle file uploads. But there are many libraries that do it. The most popular one is apache commons file uploads Here is an example on how to do that in java:

DiskFileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint
upload.setSizeMax(yourMaxRequestSize);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);

There are many more options that you should play around with.

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • Once you have the file by the way, you can do writeTo(File) and save it anywhere. – Amir Raminfar Nov 04 '10 at 22:46
  • It's not hard. It's just not builtin the standard Servlet API until version 3.0. Using 3rd party libraries in Java is as easy as dropping JAR file(s) in the classpath. – BalusC Nov 05 '10 at 01:57
  • Yep. And if you are using maven then its even easier. I was saying its hard to do it without a 3rd party. – Amir Raminfar Nov 05 '10 at 12:44
1

how does input type=file work?

This is to be used in a <form> with multipart/form-data encoding. Once a file is selected and the form is submitted, then the file contents becomes part of the HTTP request body. In the servlet, it's available as "raw" data by request.getInputStream(). In servletcontainers supporting only servlet 2.5 or older, there was no API-provided facility to parse the data. Apache Commons FileUpload is the de facto standard. Since servlet 3.0 you can use the API-provided request.getParts() for this -which is under the covers using a licensed copy of Commons FileUpload.

See also:

I still don't understand how when I submit the form a servlet will paste the image in the webpage folder and how is the value (name of the image) going to be obtained to be stored in the database table?

You should not only be interested in the file name. You should also be interested in the file contents. Whatever way you choose to parse the uploaded file out of the request body, you should end up with the file contents in flavor of an InputStream or a byte[]. You can write it to local disk file system using FileOutputStream and store the unique filename in the DB the usual JDBC way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

When a file is uploaded to a web server, the following information is usually included (or similar):

Content-Disposition: form-data; name="fileInput"; filename="myFile.txt"
Content-Type: application/octet-stream

The filename contains either the name of the file or the full path depending on the browser.

If you use a program like Fiddler, you can see exactly what's going on when you upload a file.

jordanbtucker
  • 5,768
  • 2
  • 30
  • 43