SWFUpload official sample is PHP, here to provide a Java version of the simplest use of the sample, the use of JSP page finished all operations.
Implementation of the upload, divided into three steps:
1. JavaScript Settings swfupload section (similar to the official sample):
JS Code
- var upload;
- Window.onload = function() {
- Upload = new swfupload ({
- //URL to process file uploads
- Upload_url: "${pagecontext.request.contextpath}/swfupload/example.jsp?upload=1",
- //Upload file Limit settings
- File_size_limit: "10240", //10MB
- File_Types: "*.jpg;*.gif;*.png", //Here can also be changed to the type you want to limit, for example: *.doc;*.wpd;*.pdf
- File_types_description: "Image Files",
- File_upload_limit: "0",
- File_queue_limit: "1",
- //Event handling settings (all of their own definition processing methods are in the Handler.js file)
- File_dialog_start_handler:filedialogstart,
- File_queued_handler:filequeued,
- File_queue_error_handler:filequeueerror,
- File_dialog_complete_handler:filedialogcomplete,
- Upload_start_handler:uploadstart,
- Upload_progress_handler:uploadprogress,
- Upload_error_handler:uploaderror,
- Upload_success_handler:uploadsuccess,
- Upload_complete_handler:uploadcomplete,
- //Button Settings
- Button_image_url: "Swfupload/xpbutton.png", //button icon
- button_placeholder_id: "Spanbuttonplaceholder",
- Button_width:61,
- Button_height:22,
- //SWF settings
- Flash_url: "swfupload/swfupload.swf",
- Custom_settings: {
- Progresstarget: "fsuploadprogress",
- Cancelbuttonid: "btncancel"
- },
- //Debug settings
- Debug: false
- });
- }
2, the page display part:
HTML code
- < Div class="Flash" id="fsuploadprogress" > </ Div >
- < Div style="padding-left:5px;" >
- < span id = > </ span >
- < input id = "Btncancel" type = "button" value = "Cancel" onclick = "cancelqueue (upload);"
- Disabled = "Disabled" style="MARGIN-LEFT:2PX; height:22px; font-size:8pt; " />
- </ Div >
3, the Java processing file upload part:
Java code
- String uploadsign = request.getparameter ("upload");
- String RootPath = Request.getparameter ("RootPath");
- String path = request.getparameter ("path");
- if (RootPath = = null) RootPath = "" ;
- RootPath = Rootpath.trim ();
- if (rootpath.equals ("")) {
- RootPath = Application.getrealpath ("/swfupload/files");
- }
- if (Path = = null) {
- Path = RootPath;
- }Else{
- Path = new String (Base64.decodebase64 (Path.getbytes ()));
- }
- //upload Operation
- if (null ! = uploadsign &&! ) "" . Equals (Uploadsign)) {
- Fileitemfactory factory = new diskfileitemfactory ();
- Servletfileupload upload = new servletfileupload (Factory);
- //upload.setheaderencoding ("UTF-8");
- Try {
- List items = upload.parserequest (request);
- if (null ! = items) {
- Iterator ITR = Items.iterator ();
- while (Itr.hasnext ()) {
- Fileitem item = (Fileitem) itr.next ();
- if (Item.isformfield ()) {
- Continue ;
- }Else{
- //The file name of the uploaded file with the current precision to the second date
- SimpleDateFormat sdf=New SimpleDateFormat ("Yyyymmddkkmmss");
- String type = Item.getname (). Split ("\ \" ). ) [1]; //Get file type
- File Savedfile = new file (Path,sdf.format (new Date ()) +"." +type);
- Item.write (Savedfile);
- }
- }
- }
- }catch(Exception e) {
- E.printstacktrace ();
- }
- }
SWFUpload simple use of the sample Java version (JSP)