Excel file Upload, parse, download (a file upload, using Multipartfile to achieve)

Source: Internet
Author: User

File upload I am using a jquery plug-in "Ajaxfileupload.js", the use of the way described in the following way, using the file type of input, and the need to give the button binding event, the use of "ajaxfileupload.js" here The AJAX request that is defined in the background.

<div id= "FileUpload" >    <input type= "file" id= "file" name= "file"/>    <input type= "button" id= " Upload "value=" Upload file "/> </div>
<script type= "Text/javascript" src= "<%=path%>/js/jquery/jquery-1.5.1.js" ></script><script Type= "Text/javascript" src= "<%=path%>/js/accnet/common/ajaxfileupload.js" ></script>
//File Upload$(function() {$("#upload"). Click (ajaxfileupload);});functionajaxfileupload () {varurl = "/spare/flow.spr?"; varmethod = "Method=fileupload"$.ajaxfileupload ({url:contextpath+ URL +method, Secureuri:false, Fileelementid:' File ', DataType:' Text ', Success:function(data, status) {if(data = "exist")) {alert ("This file already exists do not repeat upload"); }            if(data = "Success")) {alert ("File Upload succeeded"); }            if(data = = "Fail") {alert ("File upload failed, please re-upload"); }}, Error:function() {        }    });}

The code in the background uses the Spring MVC framework.

It also needs to be configured in the XML file when using Multipartfile.

<!--Configure Multipartresolver for file uploads using spring's commosmultipartresolver -      <Beans:beanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"p:defaultencoding= "UTF-8"p:maxuploadsize= "5400000"P:uploadtempdir= "Fileupload/temp"       >      </Beans:bean> 

The attributes are detailed in the following:
Defaultencoding= "UTF-8" is the requested encoding format, the default is Iso-8859-1
Maxuploadsize= "5400000" is the size of the uploaded file, in bytes
Uploadtempdir= "Fileupload/temp" is the temporary path for uploading files

1@RequestMapping (params = "Method=fileupload")2      Public voidFileUpload (3@RequestParam (value = "File", required =false) multipartfile file,4 httpservletrequest request, httpservletresponse response) {5String Path =request.getsession (). Getservletcontext ()6. Getrealpath ("Upload");7String FileName =file.getoriginalfilename ();8File targetfile =NewFile (path, fileName);9         //the path to the set file save is provided for subsequent parsing usingTenRequest.getsession (). SetAttribute ("Filenamespare", fileName); OneRequest.getsession (). SetAttribute ("Filepathspare", path); A         if(Targetfile.exists ()) { -             Super. Flushresponse (response, "exist"); -}Else { the             Try { - File.transferto (targetfile); -                 Super. Flushresponse (Response, "success"); -}Catch(Exception e) { + Logger.error (E.getmessage ()); -                 Super. Flushresponse (Response, "fail"); +             } A         }
at}

Here are some introductions to the Multipartfile method, pictures from the spring official website

1.getBytes () returns the contents of a file in a binary array

2.getContentType () returns the contents of the file as a string, there is some ambiguity in the method, I have not tried, like that picture, Word file type of data do not know how to handle this side

3.getInputStream () get input stream

4.getName () Gets the name of the file component in the form

5.getOriginalFilename () Get the name initialized in the client file system

6.getSize () get the file size

7.isEmpty () Determine if the file is empty

8.transferTo () Convert to File

Attach Spring's Official API documentation

Https://docs.spring.io/spring/docs/1.2.x/javadoc-api/org/springframework/web/multipart/MultipartFile.html

Excel file Upload, parse, download (a file upload, using Multipartfile to achieve)

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.