Java Background Implementation file Download function __java

Source: Internet
Author: User

The framework used in the project is Springmvc+mybatis
A feature needs to be done in the project. is a report. Then generate a file in CSV format based on the report. And then compress it. and provide download capabilities.

The business logic of the "Excel download" feature is when clicked on the button. You need to generate a CSV for the data that is displayed on the report page
Files in the format. And then do the download

How to generate a CSV (see development ...) )
This
I implement the file download function.

Our order is to enter from the foreground to the backstage in turn.
The code for the front desk is:

This is just a click event for ecxel downloads.
Attention:
Here is not used Ajax to download the file. I see people in Baidu, Bo friends said. Ajax does not provide the ability to download files with IO streaming. So most of them are dynamically adding a form forms. The form is then automatically submitted [Form.submit ()]. Automatically to the background to execute the appropriate code.

Then we need to write well our JS click event.

The logic is:
1-Declare a form forms first
2-The value that needs to be brought into the background to pass the parameter is set to the values in the input label.
3-Then [Form.submit ()] lets the form form be submitted automatically.

The next step is the processing of background events.

First of all
Generate a CSV file
and returns the path to the file after the build.

String Downloadpath = Definedtitle (list,request);

The code for downloading the file is:

FileDownload (Downloadpathfilename,response);

Specific methods to achieve:

Downloads are available for download. But I think it can be optimized.

Have thought of demo small example of not. You can leave your mailbox downstairs.

revision: 2017-03-14 19:00

Reason for revision:
File downloads in
Google browser and IE browser more than 8 is normal to download.
Problem BUG:
But in the iE8, the download is not available. And the front and back platform does not complain. This is a nerve-racking one.
The causes are:
In the IE8. Form forms cannot be implicitly committed (simply dynamically declaring a form "as above"). Be sure to display the submission (append to the page).
Solution Ideas:
1, in the IE8. You want to display the form form in the HTML file displayed.
2, and then use JS form forms automatic submission of events
3, use jquery to remove the form event. (Why do you want to remove it?) Because the more times you click. The more you add on the page. This is not advisable for the cleanliness of the code. )
4. Repair completed

The code is as follows:

Front:

Front End JS:

Back end:
No changes are required.

Effect Chart:

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.