Upload Ajax files using jquery and struts

Source: Internet
Author: User

First, let's talk about the frameworks and plug-ins used:

Struts1.3 jquery1.3 ajaxupload.3.2.js (A jquery plug-in for Ajax upload)

Cos (O 'relly's excellent Upload Component)

JSP page:

 < % @ Page Language = "Java" pageencoding = "UTF-8" % >  < % @ Include file = "../common/taglibs. jsp" % >  < ! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" >  <  Html  >    < Head  >    <  Script   Type = "Text/JavaScript"   SRC = "$ {Basepath}/script/jquery. js"  >  </  Script  >    <  Script   Type = "Text/JavaScript"  SRC = "$ {Basepath}/script/ajaxupload.3.2.js"  >  </  Script  >      <  Title  > Ajax File Upload example </  Title  >      <  Style   Type = "Text/CSS" > # Loading, Ol {font-size: 14px; display: none; color: orange; display: none;} ol {display: block ;} </  Style  >  <  Script   Type = "Text/JavaScript"  > $ (Function () {New ajaxupload ("# filebutton", {Action: "$ {basepath}/file. do? Method = upload ", autosubmit: True, name:" myfile ", onsubmit: function (file, extension) {If (extension &/^ (PDF | JPG | PNG | JPEG | GIF) $ /. test (Extension) {$ ("# loading" example .html (' < IMG   SRC = "$ {Basepath}/images/loading.gif"  > '); $ ("# Loading "). show (); $ ("# filebutton "). ATTR ("disabled", "disabled");} else {$ ("# loading" ).html ("the file you selected is not supported by the system "); $ ("# loading "). show (); Return false ;}}, oncomplete: function (file, extension) {$ ("# loading" ).html ("File Uploaded successfully "); $ ("# loading "). show (); $ ("# filebutton "). removeattr ("disabled") ;}}); New ajax_upload ('# button3', {Action: '$ {basepath}/file. do? Method = upload', name: 'myfile', autosubmit: True, oncomplete: function (file, extension) {$ (' <  Li  >  </  Li > '). Appendto ($ ('. files'). Text (File );}});}); </  Script  >    </  Head  >      <  Body  >       <  Input   Type = "Button"   Value = "Select your photo"  ID = "Filebutton"  />      <  Div   ID = "Loading"  >  <  IMG   SRC = "$ {Basepath}/images/loading.gif"  >  </  Div  >     <  HR  />         <  Form   Action = "#"   Method = "Post"  >  <  Input   ID = "Button3"   Type = "File"   /> <  P  > Successfully uploaded files include: </  P  >  <  Ol   Class = "Files"  >  </  Ol  >  <  P  > <  Input   Class = "Submit"   Type = "Submit"   Value = "Form submission"  />  </  P  >  </  Form  >    </  Body >  </  Html  > Strutsaction Code :
 Package Com. Kay. CRM. Web; Import Javax. servlet. http. httpservletrequest; Import Javax. servlet. http. httpservletresponse; Import Org. Apache. Struts. Action. actionform; Import Org. Apache. Struts. Action. actionforward; Import Org. Apache. Struts. Action. actionmapping;Import Org. Apache. Struts. Actions. dispatchaction; Import Org. springframework. stereotype. Controller; Import Com. Kay. Common. util. cosutil; @ controller (" /File ") Public   Class Fileuploadaction Extends Dispatchaction { Public Actionforward upload (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) Throws Exception {string filename = cosutil. Upload (request); system. Out. println (filename );Return   Null ;}}
 
Cos tool class:
 Package Com. Kay. Common. util; Import Java. Io. file; Import Java. Io. ioexception; Import Java. util. enumeration; Import Javax. servlet. http. httpservletrequest; Import Com. oreilly. servlet. multipartrequest; Public   Class Cosutil {@ suppresswarnings ({" Deprecation ","Unchecked "}) Public   Static String upload (httpservletrequest request) Throws Ioexception { // Save the absolute path  // String filepath = "C: // upload ";  // Save relative path String filepath = request. getrealpath (" / ") +" Upload "; File uploadpath = New File (filepath ); // Check whether the folder does not exist and create one  If (! Uploadpath. exists () {uploadpath. mkdir ();} // Maximum file capacity: 5 MB  Int Filemaxsize = 5*1024*1024; // File name String filename = Null ; // Number of uploaded files  Int Filecount = 0; // Rename the policy Randomfilerenamepolicy rfrp = New Randomfilerenamepolicy (); // Upload a file Multipartrequest mutries = New Multipartrequest (request, filepath, filemaxsize ," UTF-8 ", Rfrp); string username = muw.getparameter (" Username "); System. Out. println (username); enumeration filesname = muw.getfilenames (); While (Filesname. hasmoreelements () {string name = (string) filesname. nextelement (); filename = mutings. getfilesystemname (name); string contenttype = mutid. getcontenttype (name ); If (Filename! = Null ) {Filecount ++;} system. Out. println (" File Name: "+ Filename); system. Out. println (" File Type: "+ Contenttype);} system. Out. println ("Total uploads "+ Filecount +" Files! "); Return Filename ;}}
 
The renaming policy class used by the COs Upload Component:
 Package Com. Kay. Common. util; Import Java. Io. file; Import Java. util. date; Import Com. oreilly. servlet. multipart. filerenamepolicy; Public   Class Randomfilerenamepolicy Implements Filerenamepolicy { Public File Rename (File file) {string body ="  "; String ext ="  "; Date = New Date (); Int Pot = file. getname (). lastindexof (" . "); If (Pot! =-1) {body = date. gettime () +"  "; Ext = file. getname (). substring (POT );} Else {Body = ( New Date (). gettime () +"  "; Ext =" ";}String newname = body + ext; file = New File (file. getparent (), newname ); Return File ;}}

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.