开发者

Storing image using htm input type=file

开发者 https://www.devze.com 2023-01-23 08:39 出处:网络
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)

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 开发者_JS百科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!


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.


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:

  • How to upload files in JSP/Servlet?
  • How to retrieve uploaded file in JSP/Servlet?

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:

  • How to get full file path of uploaded file in Firefox? - to clear out a major misunderstanding


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消