Download and upload technical points and key codes for the design

Source: Internet
Author: User

First, download, generally used for data download, template download. The Excel template file (hereinafter referred to as the file) is downloaded as an example and is a MAVEN project.

1. First to explicitly place the file address on the server,

In the Java file's sibling directory, the class name, Class.getClassLoader.getResource (""), gets the file address, no problem during development, but the URL will not be found after Maven hits the war package.

The URL output is a xxx/webapp/web-inf/classes/com.xxx. Class name. Note that the development phase is no problem. Manve into the war package release, module is a separate jar package reason it.

Correct practice:

1.1. Place the template under the Src/main/resources directory template/*.xlsx of the Xxx.web module;

1.1.1 Modify Pom.xml files, supplements <exclude> tags

<build>

...

<directory>src/main/resources</directory>

<artifactId>maven-war-plugin</artifactId>

<targetPath>WEB-INF/classes</targetPath>

<excludes>

<exclude>template/*</exclude>

</excludes>

</build>

Note that the packaged template file is corrupted because the *.xlsx file is not excluded.

1.2 Getting the path code

URL FileURL = Thread.CurrentThread (). Getcontextclassload (). GetResource ("template/*.xlsx");

File File = new file (Fileurl.touri ());

Set Response header

Set the response file type (optional)

Set Response ContentLength

InputStream input = new FileInputStream (file);

Bufferedoutputstream BOS = new FileInputStream (Response.getoutputstream ());

byte[] Bytearr = new byte[2048];

while (Input.read (bytearr)! =-1) {

Bos.write (Bytearr);

}

Input.close (); Bos.close ();

Second, upload, include, file upload and Excel parsing

1. The page note that the submission form must be enctype= "Multipart/form-data" attribute.

2. If you want to add parameters, note that the browser submissions, but also the form of attachments, the background value and go to the attachment similar.

Parsing the attachment section:

Commons-fileupload-1.3.2.jar/org.apache.commons.fileupload.disk

Diskfileitemfactory factory = new Diskfileitemfactory ();

Servletfileupload upload = new Servletfileupload ();

list<fileitem> list = (list<fileitem>) upload.parserequest (request);

POI parsing Excel section:

for (Fileitem item:list) {

Inputstrem input = Item.getinputstream ();

Workbook WB = workbookfactory.create (input);

Sheet Sheet = Wb.getsheet ("Sheet1");

int rows = Sheet.getphyscalnumerofrows ();

for,rows-> Row row = Sheet.getrow (RowNum);

Row.getcell (Columnnum);

Note that Row.getcell (columnnum); The cell object that is taken out is not necessarily a string type and needs to be judged by a different type (or by looking for a value utils file processing).

}

Upload, main or check.

Download and upload technical points and key codes for the design

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.