SWFUpload simple use of the sample Java version (JSP)

Source: Internet
Author: User

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
  1. var upload;
  2. Window.onload = function() {
  3. Upload = new swfupload ({
  4. //URL to process file uploads   
  5. Upload_url: "${pagecontext.request.contextpath}/swfupload/example.jsp?upload=1",
  6. //Upload file Limit settings   
  7. File_size_limit: "10240", //10MB
  8. File_Types: "*.jpg;*.gif;*.png", //Here can also be changed to the type you want to limit, for example: *.doc;*.wpd;*.pdf
  9. File_types_description: "Image Files",
  10. File_upload_limit: "0",
  11. File_queue_limit: "1",
  12. //Event handling settings (all of their own definition processing methods are in the Handler.js file)   
  13. File_dialog_start_handler:filedialogstart,
  14. File_queued_handler:filequeued,
  15. File_queue_error_handler:filequeueerror,
  16. File_dialog_complete_handler:filedialogcomplete,
  17. Upload_start_handler:uploadstart,
  18. Upload_progress_handler:uploadprogress,
  19. Upload_error_handler:uploaderror,
  20. Upload_success_handler:uploadsuccess,
  21. Upload_complete_handler:uploadcomplete,
  22. //Button Settings   
  23. Button_image_url: "Swfupload/xpbutton.png", //button icon
  24. button_placeholder_id: "Spanbuttonplaceholder",
  25. Button_width:61,
  26. Button_height:22,
  27. //SWF settings   
  28. Flash_url: "swfupload/swfupload.swf",
  29. Custom_settings: {
  30. Progresstarget: "fsuploadprogress",
  31. Cancelbuttonid: "btncancel"
  32. },
  33. //Debug settings   
  34. Debug: false
  35. });
  36. }

2, the page display part:

HTML code
  1. < Div class="Flash" id="fsuploadprogress" > </ Div >   
  2. < Div style="padding-left:5px;" >   
  3.      < span   id = > </ span >   
  4.      < input   id = "Btncancel"   type = "button"   value = "Cancel"   onclick = "cancelqueue (upload);"    
  5. Disabled = "Disabled"  style="MARGIN-LEFT:2PX; height:22px; font-size:8pt; " />
  6. </ Div >   

3, the Java processing file upload part:

Java code
  1. String uploadsign = request.getparameter ("upload");
  2. String RootPath = Request.getparameter ("RootPath");
  3. String path = request.getparameter ("path");
  4. if (RootPath = = null) RootPath = ""  ;
  5. RootPath = Rootpath.trim ();
  6. if (rootpath.equals ("")) {
  7. RootPath = Application.getrealpath ("/swfupload/files");
  8. }
  9. if (Path = = null) {
  10. Path = RootPath;
  11. }Else{
  12. Path = new String (Base64.decodebase64 (Path.getbytes ()));
  13. }
  14. //upload Operation   
  15. if (null ! = uploadsign &&! ) "" . Equals (Uploadsign)) {  
  16. Fileitemfactory factory = new diskfileitemfactory ();
  17. Servletfileupload upload = new servletfileupload (Factory);
  18. //upload.setheaderencoding ("UTF-8");   
  19. Try {  
  20. List items = upload.parserequest (request);
  21. if (null ! = items) {
  22. Iterator ITR = Items.iterator ();
  23. while (Itr.hasnext ()) {
  24. Fileitem item = (Fileitem) itr.next ();
  25. if (Item.isformfield ()) {
  26. Continue  ;
  27. }Else{
  28. //The file name of the uploaded file with the current precision to the second date   
  29. SimpleDateFormat sdf=New SimpleDateFormat ("Yyyymmddkkmmss");
  30. String type = Item.getname (). Split ("\ \" ). ) [1]; //Get file type   
  31. File Savedfile = new file (Path,sdf.format (new Date ()) +"."  +type);
  32. Item.write (Savedfile);
  33. }
  34. }
  35. }
  36. }catch(Exception e) {
  37. E.printstacktrace ();
  38. }
  39. }

SWFUpload simple use of the sample Java version (JSP)

Related Article

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.