Java uses SWFUpload to upload multiple files. The session is empty and cannot be verified.

Source: Internet
Author: User

We all know that normal file uploads are files uploaded through forms and cannot be uploaded asynchronously. Some technical means such as jquery form. js can be used for asynchronous upload, but the most important problem is that it cannot upload multiple files. If you want to upload multiple files, you must upload them one by one. At the same time, you must handle the problem of the next file upload box after a file is uploaded.
Now we have more operations, that is, using swfupload for asynchronous Multifile upload. As the name suggests, it is a flash upload tool, but the display form on the interface makes it the same as ordinary html elements, without complicated display, just a normal upload box, to achieve the desired purpose.

There is no need to use swfupload here. Here we mainly introduce how to solve the verification failure problems frequently encountered during java web development. This is because flash uses different sessions with the browser during the upload. flash SWFUpload uses socket Sockets for communication, so the session is different from the previous one, this results in session loss. In this way, the server is considered as a new session during verification, and thus the verification fails, resulting in the upload failure.
The solution is to enable flash to carry the session ID on the same interface when uploading files. This is usually done by modifying the upload_url. The modification is as follows:

upload_url: "/admin/infobuild/image/upload.action;jsessionid=${pageContext.session.id }"

The above sentence must be placed on the jsp interface, because the Java Web syntax is used in it. Of course, this can be modified by setting <% = session. getId () %> or $ {pageContext. session. id} is placed in a special js variable to hide the variable. For example, the following code:

<! -- This sentence should be included in the script on the jsp page of the house or in the script in the servlet response. getWriter () stream. In short, it should be executed before the js introduction of swfupload -->

window["sessionId"]="${pageContext.session.id}"; 

<! -- This sentence can be used in the introduced js -->

upload_url: Gtip.util.addContextPath("/admin/infobuild/image/upload.action;jsessionId"  + "=" + window["sessionId"]),

The preceding solution is to solve the java variable values on the jsp page;

Put the first sentence of the above Code in the jsp file, such as the jsp public include file. Put the following sentence in the js file, such as the introduced upload. js file.
However, the above Code still has a problem, that is, the default sessionName. In tomcat or other j2ee containers, the default sessionName is also jsessionId. If sessionName is modified, for example, session_cookie_name is modified through tomcat server. xml, the default sessionName is what we modified. To solve this problem, you need to add a parameter setting, as shown below:

 

<! -- Put the three rows on the jsp page imported by include -->

window["contextPath"] = "${pageContext.request.contextPath}";

window["sessionId"] = "${pageContext.session.id}";

window["sessionName"] = "jsessionId";

 

// Use the window variable value set in jsp in js

upload_url: addContextPath("/admin/infobuild/image/upload.action;" + window["sessionName"] + "=" + window["sessionId"])

The first three lines of the above Code are put in the public include. jsp, And the last line is put in the public upload. js. In this way, the final requirement can be met.
However, if sessionName is modified in the deployment environment, you must modify the sessionName in this include. jsp file.

Reprinted please indicate the source: I flym
Address: http://www.iflym.com/index.php/code/201108190001.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.