Some problems of file upload form processing in gwa2 Java


In gwa2java, processing the HTML form of file upload is called “mixed loading of passenger and freight” HTTP request processing.

File upload processing is a special application in HTTP web development, which has certain requirements for developers. Whether it’s popular PHP, ASPX or JSP, you need some processing skills. Since I really can’t remember how to handle the HTML file upload form of mixed loading of passenger and freight with bare hands and pure code more than 10 years ago, I’d like to record it in detail this time for future reference.

Another consideration is that when dealing with HTML file upload forms, we need to consider the integration with gwa2 Java, which involves the core module of gwa2 file processing. Therefore, we also need or is an excellent opportunity to review gwa2 architecture design.
During the inspection of the previous gwa2 java project, we found that there are some projects involving file upload, but most of them use ajax to upload files asynchronously, avoiding the mode of “mixed loading of passengers and goods”. One HTML form is only responsible for file upload, while another form collects other data.

Therefore, this time we need to better integrate the file upload in gwa2java, and it is a mixed mode of passenger and freight loading – processing a form, which includes two fields at the same time: Zhang San’s name text and Zhang San’s head picture.

1. Enctype in HTML form of browser client

If you need to upload a file in the HTML form of the browser client, you need to explicitly specify that the enctype attribute of the form element is multipart / form data.

2. The server receives and processes the third-party component of file upload

Apache commons-fileupload-1.4.jar is recommended for server-side file upload based on Apache Tomcat Java Web server, and Apache commons-io-2.7.jar is also recommended.

3. HTTP Request.getParameter invalid

When the client uses the form uploaded by file to submit data, the main method of the normal servlet object httprequest in Java Web server request.getParameter It’s not working. This is also one of the reasons why ordinary procedures cannot handle mixed loading of passengers and goods.

Instead, when enctype = multipart / form data, the corresponding form parameters need to be obtained through a third-party component.
In gwa2, the routing module relies heavily on the parameters of HTTP request. At this time, we need to put the main routing parameters into the action attribute of form.
Another method that can be improved is to introduce the related objects of Apache Commons fileUpload to facilitate the formitems object, so as to obtain the names and values of general non file fields and text file fields in the form.

After traversing the formitems to obtain the HTTP request parameters, the request.setAttribute Write back to the current request object, so you can make up for it cleverly request.getParameter The problem of failure.
In gwa2java, getting external parameters is usually done by Wht.get In addition to calling request.getParameter In addition, there will be further visits request.getAttribute In order to achieve the goal of request.setAttribute Visit. Further Wht.get We will also do some basic security checks on the access data.

Sample 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 form upload request can and can only be parsed once

As mentioned above, getting formitems needs to be extracted from HTTP request,

List<FormItem> formItems = uploadInstance.parseRequest(request);

This request can only be executed once by parserequest. If parserequest is executed again for this request in the same request handler, an error with null exception will be returned.
Therefore, when we need to set the form items at the top of the program by convenience request.setAttribute Formitems should also be retained as a global object when processing the actual uploaded file in the following section to avoid calling parserequest again to generate an empty error exception.

5. Lingering character transcoding: from iso-8859-1 to UTF-8

From the heart, we are resistant to transcoding the character set in the code, so from the beginning, we unified the front and back ends in gwa2java, and consistently used UTF-8 character set in the whole process, so as to avoid unnecessary transcoding in the system program. (refer to the problem of I18N / Chinese encoding / garbled code in – gwa2 java version,

However, when the mixed loading mode is used for file upload, the encoding of non ASCII characters in the form item generated by parserequest is iso-8859-1. For the default UTF-8 environment like gwa2java, the transcoding of character set becomes a must.

6. File upload processing method

In the architecture design of gwa2, the file upload function belongs to the file module. The interface file introduced into the system is Inc / filea. For different file systems, there are different file classes to implement Inc / filedirver. For example, the default file processing is Linux Inc / filesystem

In terms of attribution, the classes and methods under the Inc directory are “ladies of the family”. Generally, they do not participate in specific business processes, and can only be called through the unified interface Inc / webapp. Therefore, the ideal logical process of calling file upload is roughly as follows:

ctrl/a.jsp –> mod/ModA –> inc/WebApp –> inc/FileA –> inc/FileSystem .

Because file upload is an unconventional “operation” requirement, another nonstandard shortcut is to directly instantiate Inc / filesystem in mod / Moda, so as to easily trigger the application of upload method. But this is only feasible for specific projects, and does not need portability. For example, if the host system is not Linux, the Inc / file system cannot be instantiated. Accordingly, the Inc / windows file system may need to be instantiated to handle the file upload when the host environment is windows.

7. There are in and out, file deletion operation

Previously, readObject / writeobject was defined in the core interface class Inc / webapp to read and write data from non database sources. The read / write method is defined in Inc / filea, but there is no implementation of RM.

Next, we implement RM methods of three classes, Inc / filea, Inc / filedriver and inc / filesystem, and transform Inc/ WebApp.rmBy Methods the above methods were connected.

inc/WebApp.rmBy(“file:Path_To_File”) –> inc/FileA.rm –> inc/FileSystem.rm .

Gwa2java, gwa2 is like this, through the practice of one project after another, constantly enrich and improve, become increasingly robust and powerful, when the integration and precipitation of enough wisdom and essence, we will achieve greater success. We are in the ufqiwork workshop( this paper, the mixed loading mode of gwa2java processing file upload is implemented.

Some problems of file upload form processing in gwa2 Java

-Gwa2 Jiwa rabbitIt is the general web application architecture”Based on – gwa2, various network applications can be easily built,
It includes complex online shopping malls, online health care, online education, tourism trading platforms, social or social networking sites, news and information sites, etc,
Also includes various enterprises and institutions online portal, online interaction and service operating system
It can also include server support for native app, even all of webapp
-Gwa2 is one of the few application frameworks that support cross development languages. At present, gwa2 supports – Java, – PHP, – Perl, – ASPX and – Python

-GWA2 is a “General Web Application Architecture” and based on -GWA2 developers can easily build a variety of network applications,
including complex online shopping malls, online medical services, online teaching, travel trading platforms, community or social networking sites and news information sites, etc.
Also the applications include various online portals of enterprises and institutions, online interaction and service operations systems.
Moreover it contains server-side support for NativeApp, or even all of the WebApp.
-GWA2 is one of the web frameworks which provide cross-language support for -Java, -PHP, -Perl, -Aspx and -Python at present.

-GWA2 is E.A.S.Y
**Easy Along, Swift Yield
**Easy start, fast output

Some problems of file upload form processing in gwa2 Java

Ufqiwork is an online service trading platform. (
Youfu workshop provides online classified service information, and is committed to matching buyers and sellers of online service transactions. It also provides “banking standard” services for buyers and sellers and mediates guarantee service transactions. The provider of banking standard service is the third party of intermediary transaction. Youfu workshop’s service trading platform is the Fourth Party of the whole service trading process.


Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]