简体   繁体   English

使用htm输入类型=文件存储图像

[英]Storing image using htm input type=file

Hello I'm building a web application (online store) where an admin can upload a new product. 您好,我正在构建一个Web应用程序(在线商店),管理员可以在其中上传新产品。 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? 我的问题是,输入type = file如何工作? 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? 我仍然不明白,当我提交表单时,servlet如何将图像粘贴到webpage文件夹中,以及如何获取值(图像名称)以存储在数据库表中?

For the other inputs i use "value" to get the info. 对于其他输入,我使用“值”来获取信息。

Thanks! 谢谢!

In java, its hard to do handle file uploads. 在Java中,很难处理文件上传。 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: 最受欢迎的是apache commons文件上传这是一个有关如何在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? 输入type = file如何工作?

This is to be used in a <form> with multipart/form-data encoding. 这将在具有multipart/form-data编码的<form>使用。 Once a file is selected and the form is submitted, then the file contents becomes part of the HTTP request body. 选择文件并提交表单后,文件内容将成为HTTP请求正文的一部分。 In the servlet, it's available as "raw" data by request.getInputStream() . 在Servlet中, request.getInputStream()可将其作为“原始”数据使用。 In servletcontainers supporting only servlet 2.5 or older, there was no API-provided facility to parse the data. 在仅支持servlet 2.5或更早版本的servlet容器中,没有API提供的工具来解析数据。 Apache Commons FileUpload is the de facto standard. Apache Commons FileUpload是事实上的标准。 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. 从Servlet 3.0开始,您可以为此使用API​​提供的request.getParts() -使用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? 我仍然不明白,当我提交表单时,servlet将如何将图像粘贴到webpage文件夹中,以及如何获取值(图像名称)以存储在数据库表中?

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[] . 无论选择哪种方法从请求正文中解析上载的文件,都应该以InputStreambyte[]结束文件内容。 You can write it to local disk file system using FileOutputStream and store the unique filename in the DB the usual JDBC way. 您可以使用FileOutputStream将其写入本地磁盘文件系统,并以通常的JDBC方法将唯一的文件名存储在DB中。

See also: 也可以看看:

When a file is uploaded to a web server, the following information is usually included (or similar): 将文件上传到Web服务器时,通常会包含以下信息(或类似信息):

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. filename包含文件名或完整路径,具体取决于浏览器。

If you use a program like Fiddler , you can see exactly what's going on when you upload a file. 如果使用Fiddler之类的程序,则可以确切地看到上载文件时的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM