In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java file upload form problem how to deal with", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java file upload form problem how to deal with" it!
HTML lists uploaded to files in GWA2Java are considered to be "customer mixed" HTTP requests.
File upload and processing is a special application in HTTP Web development and has certain requirements for developers. Both popular PHP,Aspx and JSP require some processing skills. Since I really can't remember how to process the upload form list of mixed customer HTML files with bare hands more than 10 years ago, this time I kept a record so that I could forget about it and check it later.
Another test is that when processing HTML documents, the integration of examination and GWA2Java is needed, which involves the core module of GWA2 document processing, so it also needs or is an excellent opportunity to check the design of GWA2 framework.
During the inspection of previous GWA2 Java projects, we found that there were some items related to file upload, but basically, most of them used Ajax last time, avoiding the "customer mixed" mode. One HTML list only uploaded files, while another form collected other data.
Therefore, this time to better integrate the file upload in GWA2Java, and in the customer mixed mode-processing a list, the face also includes two fields: the name text of the third and the image of the third.
1. Enctype in the HTML list on the client side of the client
If you need to upload files in the HTML list on the client side of the client, you need to explicitly specify the enctype attribute of the Form element to multipart/form-data.
two。 The server receives the third-party components uploaded to the processing file
Based on the Java Web Server of Apache Tomcat, the server-side file upload and processing is carried out, and the component used is Apache commons-fileupload-1.4.jar, together with the introduction of Apache commons-io-2.7.jar.
3. HTTP Request.getParameter failure
When the client uses the number of form lists uploaded to the file, request.getParameter, the main method of the regular Servlet object HTTPRequest in Java Web Server, becomes invalid. This is also one of the reasons why ordinary programs are unable to handle customer mixing.
Instead, enctype=multipart/form-data needs to fetch the corresponding table parameters through third-party components.
In GWA2, the routing module relies heavily on the parameters of the HTTP Request. At this point, you need to put the main routing parameters into the Action property of the Form.
Another method that can be improved is to obtain the names and values of regular non-file fields and text file fields in the table by introducing related objects of Apache commons-fileupload to facilitate FormItems objects.
After getting the HTTP Request parameters through FormItems, and then writing back to the pre-Request object via request.setAttribute, you can skillfully solve the problem of request.getParameter failure.
Because in GWA2Java, fetching external parameters is usually done by the Wht.get method, in addition to using request.getParameter, this method will further analyze the request.getAttribute, thus realizing the transformation of the request.setAttribute. Further, the Wht.get will do some basic security checks on the data.
Case code:
ServletFileUpload sfileupld = new ServletFileUpload ((new DiskFileItemFactory ()
FormItems = sfileupld.parseRequest (request); / / can only be parsed once!
If (formItems! = null & & formItems.size () > 0) {
String iname, ivalue; byte [] bytes
For (FileItem item: formItems) {
/ / processes only fields that are common form fields
If (item.isFormField ()) {
Bytes = item.getFieldName () .getBytes ("ISO-8859-1"); / / why 8859?
Iname = new String (bytes, "UTF-8")
Bytes = item.getString () .getBytes ("ISO-8859-1")
Ivalue = new String (bytes, "UTF-8")
Request.setAttribute (iname, ivalue)
/ / debug ("ctrl/item: iname:" + iname+ ", ivalue:" + ivalue)
}
}
}
4. HTML list upload request can and can be parse once
The fetching FormItems mentioned above needs to be extracted from HTTP Request.
List formItems = uploadInstance.parseRequest (request)
The Request here can only be executed by parseRequest once. If you run parseRequest on this request again in the same request, it will return an error that is often empty.
Therefore, when we need to configure request.setAttribute through convenient FormItems in the program, we also need to keep FormItems as the global object and use it later when processing actual uploaded documents, so as to avoid creating empty errors with parseRequest again.
5. Character transcoding that cannot be removed: from ISO-8859-1 to UTF-8
At heart, we are resistant to character set conversion in the code, so from the beginning, we have carried out a unified process of using the UTF-8 character set in front and back in GWA2Java, thus avoiding unnecessary coding work in the system program. (reference:-GWA2 Java version of i18n/ Chinese coding / garbled problem, https://ufqi.com/blog/gwa2-java-i18n/)
However, when this server uses the customer mixed mode to upload files, the encoding of non-ASCII characters in the FormItem via parseRequest is ISO-8859-1. For a default UTF-8 environment like GWA2Java, character set conversion is a last resort.
6. Installation of file upload processing methods
In the architecture design of GWA2, the file upload function is restricted to the file model, and the interface file introduced into the system is inc/FileA. For different file systems, there are different file types to implement inc/FileDirver. For example, the default file processing is Linux inc/FileSystem.
On the Internet, these categories and methods under the inc directory are "everyone's show". Generally, those who do not participate in the specific transaction processing program can only be used through the unified interface inc/WebApp. Therefore, ideally, the process of uploading a usage file is roughly as follows:
Ctrl/a.jsp-> mod/ModA-> inc/WebApp-> inc/FileA-> inc/FileSystem.
Due to file uploads, due to unusual "operation" requirements, another non-standard shortcut is to directly instantiate inc/FileSystem in mod/ModA to facilitate the use of upload methods. However, this is only feasible for hardware projects and does not require portability. For example, if the host system is not Linux, then inc/FileSystem cannot be instantiated. Accordingly, inc/WindowsFileSystem may need to be instantiated to process file uploads in which the host environment is Windows.
7. The operation of deleting files in and out.
Prior to this, the data used by readObject/writeObject to write non-data sources was defined in the core interface class inc/WebApp. The read/write method is defined correspondingly in inc/FileA, but there is no implementation of the corresponding division method rm.
Then, we introduce and implement three kinds of rm methods: inc/FileA, inc/FileDriver and inc/FileSystem. At the same time, we modify the inc/WebApp.rmBy method to connect the above methods.
Inc/WebApp.rmBy ("file:Path_To_File")-> inc/FileA.rm-> inc/FileSystem.rm.
In this way, through one project after another, GWA2Java and GWA2 will be able to achieve greater achievements if they are not rich and perfect, become stronger and stronger day by day, and integrate and immerse themselves in a lot of wisdom and excellence. We first implemented the customer mixed mode of uploading GWA2Java processing files on UfqiWork Youfu Workshop (https://ufqi.com/work/).
Thank you for reading, the above is the content of "how to deal with the file upload form problem in Java". After the study of this article, I believe you have a deeper understanding of how to deal with the file upload form problem in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.