[Transfer] SWFUpload + COS + servelet upload component upload large attachments to achieve

First, the needs and background: the company had net monthly meeting of the document much more than 50M, the original upload component using struts upload + io flows through the General Assembly if the uploaded file causes a memory overflow. And there is also an urgent customer's wish to have a big attachment upload features.

Second,

1, [swfupload] Component Download http://swfupload.googlecode.com ( SWFUpload v2.2.0.1 Core.zip )

2, [COS upload component] Download http://www.servlets.com/cos/index.html (COS-26Dec2008.zip)

3, swfupload and cos integration

The first step: let swfupload run, write index.jsp:

view plain copy to clipboard print?
  1. <% @ Page language = "java" contentType = "text / html; charset = utf-8" pageEncoding = "utf-8"%>
  2. <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title> upload large attachments - Simple Demo </ title>
  6. <link href="css/default.css" mce_href="css/default.css" rel="stylesheet" type="text/css" />
  7. <mce:script type="text/javascript" src="js/swfupload/swfupload.js" mce_src="js/swfupload/swfupload.js"> </ mce: script>
  8. <mce:script type="text/javascript" src="js/swfupload/swfupload.queue.js" mce_src="js/swfupload/swfupload.queue.js"> </ mce: script>
  9. <mce:script type="text/javascript" src="js/swfupload/fileprogress.js" mce_src="js/swfupload/fileprogress.js"> </ mce: script>
  10. <mce:script type="text/javascript" src="js/swfupload/handlers.js" mce_src="js/swfupload/handlers.js"> </ mce: script>
  11. <mce:script type="text/javascript"> <! -
  12. var swfu;
  13. window.onload = function () {
  14. var settings = {
  15. flash_url: "swfupload / swfupload.swf",
  16. upload_url: "up / upload.jsp",
  17. post_params: {"uploadTimer": new Date ()},
  18. file_size_limit: "200 MB",
  19. file_types: "*.*",
  20. file_types_description: "All Files",
  21. file_upload_limit: 100,
  22. file_queue_limit: 0,
  23. custom_settings: {
  24. progressTarget: "fsUploadProgress",
  25. cancelButtonId: "btnCancel",
  26. upload_successful: false
  27. }
  28. debug: false,
  29. / / Button settings
  30. button_image_url: "images/XPButtonUploadText_61x22.png",
  31. button_placeholder_id: "spanButtonPlaceholder",
  32. button_width: 61,
  33. button_height: 22,
  34. / / The event handler functions are defined in handlers.js
  35. file_queued_handler: fileQueued,
  36. file_queue_error_handler: fileQueueError,
  37. file_dialog_complete_handler: fileDialogComplete,
  38. upload_start_handler: uploadStart,
  39. upload_progress_handler: uploadProgress,
  40. upload_error_handler: uploadError,
  41. upload_success_handler: uploadSuccess,
  42. upload_complete_handler: uploadComplete,
  43. queue_complete_handler: queueComplete / / Queue plugin event
  44. };
  45. swfu = new SWFUpload (settings);
  46. };
  47. / / --></ Mce: script>
  48. </ Head>
  49. <body>
  50. <div>
  51. <h2> </ h2>
  52. <form action="index.jsp" method="post" enctype="multipart/form-data">
  53. <p> This page demonstrates a simple usage of SWFUpload. It uses the Queue Plugin to simplify uploading or cancelling all queued files. </ p>
  54. <div>
  55. <span> Upload Queue </ span>
  56. </ Div>
  57. <div> 0 Files Uploaded </ div>
  58. <div>
  59. <span> </ span>
  60. <input type="button" value="Cancel All Uploads" disabled="disabled" />
  61. </ Div>
  62. </ Form>
  63. </ Div>
  64. </ Body>
  65. </ Html>

<% @ Page language = "java" contentType = "text / html; charset = utf-8" pageEncoding = "utf-8"%> <! DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title > Upload large attachments - Simple Demo </ title> <link href = "http://ljhmustang.blog.163.com/blog/css/default.css" mce_href = "http://ljhmustang.blog.163. com / blog / css / default.css "rel =" stylesheet "type =" text / css "/> <mce: script type =" text / javascript "src =" http://ljhmustang.blog.163.com/ blog / js / swfupload / swfupload.js "mce_src =" http://ljhmustang.blog.163.com/blog/js/swfupload/swfupload.js "> </ mce: script> <mce: script type =" text / javascript "src =" http://ljhmustang.blog.163.com/blog/js/swfupload/swfupload.queue.js "mce_src =" http://ljhmustang.blog.163.com/blog/js/swfupload / swfupload.queue.js "> </ mce: script> <mce: script type =" text / javascript "src =" http://ljhmustang.blog.163.com/blog/js/swfupload/fileprogress.js " mce_src = "http://ljhmustang.blog.163.com/blog/js/swfupload/fileprogress.js"> </ mce: script> <mce: script type = "text / javascript" src = "http:// ljhmustang.blog.163.com/blog/js/swfupload/handlers.js "mce_src =" http://ljhmustang.blog.163.com/blog/js/swfupload/handlers.js "> </ mce: script> <mce:script type="text/javascript"> <! - var swfu; window.onload = function () {var settings = {flash_url: "swfupload / swfupload.swf", upload_url: "up / upload.jsp" , post_params: {"uploadTimer": new Date ()}, file_size_limit: "200 MB", file_types: "*.*", file_types_description: "All Files", file_upload_limit: 100, file_queue_limit: 0, custom_settings: {progressTarget: " fsUploadProgress ", cancelButtonId:" btnCancel ", upload_successful: false}, debug: false, / / Button settings button_image_url:" images/XPButtonUploadText_61x22.png ", button_placeholder_id:" spanButtonPlaceholder ", button_width: 61, button_height: 22, / / The event handler functions are defined in handlers.js 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, queue_complete_handler: queueComplete / / Queue plugin event}; swfu = new SWFUpload (settings);}; / / --></ mce: script> </ head> <body> <div> <h2> </ h2> <form action = "index.jsp" method = " post "enctype =" multipart / form-data "> <p> This page demonstrates a simple usage of SWFUpload. It uses the Queue Plugin to simplify uploading or cancelling all queued files. </ p> <div> <span> Upload Queue </ span> </ div> <div> 0 Files Uploaded </ div> <div> <span> </ span> <input type="button" value="Cancel All Uploads" disabled="disabled" /> < / div> </ form> </ div> </ body> </ html>
Note: 1, all the js and css files are downloaded from the official website, and no particular change.

2, upload_url: "up / upload.jsp", where "up / upload.jsp" can be uploaded using a jsp page class to write (cos), I use servelet path configuration

2, web.xm configuration:

view plain copy to clipboard print?
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  3. <display-name> upload </ display-name>
  4. <welcome-file-list>
  5. <welcome-file> index.html </ welcome-file>
  6. <welcome-file> index.htm </ welcome-file>
  7. <welcome-file> index.jsp </ welcome-file>
  8. <welcome-file> default.html </ welcome-file>
  9. <welcome-file> default.htm </ welcome-file>
  10. <welcome-file> default.jsp </ welcome-file>
  11. </ Welcome-file-list>
  12. <servlet>
  13. <servlet-name> SimpleUploader </ servlet-name>
  14. <servlet-class> com.servlet.uploader.SimpleUploaderServlet </ servlet-class>
  15. <init-param>
  16. <param-name> fileSizeLimit </ param-name>
  17. <param-value> 500 </ param-value>
  18. </ Init-param>
  19. </ Servlet>
  20. <servlet-mapping>
  21. <servlet-name> SimpleUploader </ servlet-name>
  22. <url-pattern> / up / upload.jsp </ url-pattern>
  23. </ Servlet-mapping>
  24. </ Web-app>

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance "xsi: schemaLocation =" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/ j2ee/web-app_2_4.xsd "> <display-name> upload </ display-name> <welcome-file-list> <welcome-file> index.html </ welcome-file> <welcome-file> index.htm </ welcome-file> <welcome-file> index.jsp </ welcome-file> <welcome-file> default.html </ welcome-file> <welcome-file> default.htm </ welcome-file> <welcome -file> default.jsp </ welcome-file> </ welcome-file-list> <servlet> <servlet-name> SimpleUploader </ servlet-name> <servlet-class> com.servlet.uploader.SimpleUploaderServlet </ servlet -class> <init-param> <param-name> fileSizeLimit </ param-name> <param-value> 500 </ param-value> </ init-param> </ servlet> <servlet-mapping> <servlet- name> SimpleUploader </ servlet-name> <url-pattern> / up / upload.jsp </ url-pattern> </ servlet-mapping> </ web-app>
Note: 1, fileSizeLimit is configured to allow upload size 500, on behalf of 500M.

3, the preparation of SimpleUploaderServlet class (using cos.jar upload):

view plain copy to clipboard print?
  1. package com.servlet.uploader;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Enumeration;
  5. import javax.servlet.ServletConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import org.apache.commons.io.FilenameUtils;
  12. import org.apache.commons.lang.StringUtils;
  13. import org.apache.commons.lang.time.DateFormatUtils;
  14. import com.oreilly.servlet.MultipartRequest;
  15. import com.oreilly.servlet.multipart.FileRenamePolicy;
  16. public class SimpleUploaderServlet extends HttpServlet {
  17. private static final long serialVersionUID =-3096800116651263134L;
  18. private String fileSizeLimit;
  19. public void init (ServletConfig config) throws ServletException {
  20. this.fileSizeLimit = config.getInitParameter ("fileSizeLimit");
  21. }
  22. public void destroy () {
  23. this.fileSizeLimit = null;
  24. super.destroy ();
  25. }
  26. class MyFileRenamePolicy implements FileRenamePolicy {
  27. public File rename (File file) {
  28. String fileSaveName = StringUtils.join (new String [] {java.util.UUID.randomUUID (). ToString (), ".",
  29. FilenameUtils.getExtension (file.getName ())});
  30. File result = new File (file.getParentFile (), fileSaveName);
  31. return result;
  32. }
  33. }
  34. public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. System.out.println ("--- BEGIN DOPOST ---");
  36. HttpSession session = request.getSession ();
  37. String uploadDir = "files" + File.separatorChar + "upload" + File.separatorChar;
  38. String autoCreatedDateDirByParttern = "yyyy" + File.separatorChar + "MM" + File.separatorChar + "dd"
  39. + File.separatorChar;
  40. String autoCreatedDateDir = DateFormatUtils.format (new java.util.Date (), autoCreatedDateDirByParttern);
  41. String ctxDir = session.getServletContext (). GetRealPath (String.valueOf (File.separatorChar));
  42. if (! ctxDir.endsWith (String.valueOf (File.separatorChar))) {
  43. ctxDir = ctxDir + File.separatorChar;
  44. }
  45. File savePath = new File (ctxDir + uploadDir + autoCreatedDateDir);
  46. if (! savePath.exists ()) {
  47. savePath.mkdirs ();
  48. }
  49. String saveDirectory = ctxDir + uploadDir + autoCreatedDateDir;
  50. if (StringUtils.isBlank (this.fileSizeLimit.toString ())) {
  51. this.fileSizeLimit = "100"; / / default 100M
  52. }
  53. int maxPostSize = Integer.valueOf (this.fileSizeLimit). intValue () * 1024 * 1024;
  54. String encoding = "GBK";
  55. FileRenamePolicy rename = new MyFileRenamePolicy ();
  56. MultipartRequest multi = null;
  57. try {
  58. multi = new MultipartRequest (request, saveDirectory, maxPostSize, encoding, rename);
  59. } Catch (IOException e) {
  60. System.out.println (e.getMessage ());
  61. return;
  62. }
  63. / / Output
  64. Enumeration files = multi.getFileNames ();
  65. while (files.hasMoreElements ()) {
  66. String name = (String) files.nextElement ();
  67. File f = multi.getFile (name);
  68. if (f! = null) {
  69. String fileName = multi.getFilesystemName (name);
  70. String lastFileName = saveDirectory + "\ \" + fileName;
  71. String fileSavePath = uploadDir + autoCreatedDateDir + fileName;
  72. System.out.println ("SimpleUploaderServlet");
  73. System.out.println ("File Address:" + lastFileName);
  74. System.out.println ("Save the path:" + fileSavePath);
  75. }
  76. }
  77. System.out.println ("--- END DOPOST ---");
  78. }
  79. public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  80. this.doPost (request, response);
  81. }
  82. public String getFileSizeLimit () {
  83. return fileSizeLimit;
  84. }
  85. public void setFileSizeLimit (String fileSizeLimit) {
  86. this.fileSizeLimit = fileSizeLimit;
  87. }
  88. }

package com.servlet.uploader; import java.io.File; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet ; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateFormatUtils; import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.FileRenamePolicy; public class SimpleUploaderServlet extends HttpServlet {private static final long serialVersionUID =-3096800116651263134L; private String fileSizeLimit; public void init (ServletConfig config) throws ServletException {this.fileSizeLimit = config.getInitParameter ("fileSizeLimit");} public void destroy () {this.fileSizeLimit = null; super.destroy ();} class MyFileRenamePolicy implements FileRenamePolicy { public File rename (File file) {String fileSaveName = StringUtils.join (new String [] {java.util.UUID.randomUUID (). toString (), ".", FilenameUtils.getExtension (file.getName ())}) ; File result = new File (file.getParentFile (), fileSaveName); return result;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println ("--- BEGIN DOPOST - - "); HttpSession session = request.getSession (); String uploadDir =" files "+ File.separatorChar +" upload "+ File.separatorChar; String autoCreatedDateDirByParttern =" yyyy "+ File.separatorChar +" MM "+ File.separatorChar + "dd" + File.separatorChar; String autoCreatedDateDir = DateFormatUtils.format (new java.util.Date (), autoCreatedDateDirByParttern); String ctxDir = session.getServletContext (). getRealPath (String.valueOf (File.separatorChar)); if (! ctxDir.endsWith (String.valueOf (File.separatorChar))) {ctxDir = ctxDir + File.separatorChar;} File savePath = new File (ctxDir + uploadDir + autoCreatedDateDir); if (! savePath.exists ()) {savePath . mkdirs ();} String saveDirectory = ctxDir + uploadDir + autoCreatedDateDir; if (StringUtils.isBlank (this.fileSizeLimit.toString ())) {this.fileSizeLimit = "100"; / / default 100M} int maxPostSize = Integer.valueOf (this.fileSizeLimit). intValue () * 1024 * 1024; String encoding = "GBK"; FileRenamePolicy rename = new MyFileRenamePolicy (); MultipartRequest multi = null; try {multi = new MultipartRequest (request, saveDirectory, maxPostSize, encoding, rename );} catch (IOException e) {System.out.println (e.getMessage ()); return;} / / output Enumeration files = multi.getFileNames (); while (files.hasMoreElements ()) {String name = ( String) files.nextElement (); File f = multi.getFile (name); if (f! = null) {String fileName = multi.getFilesystemName (name); String lastFileName = saveDirectory + "\ \" + fileName; String fileSavePath = uploadDir + autoCreatedDateDir + fileName; System.out.println ("SimpleUploaderServlet"); System.out.println ("File Address:" + lastFileName); System.out.println ("Save the path:" + fileSavePath);}} System.out.println ("--- END DOPOST ---");} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost (request, response);} public String getFileSizeLimit () { return fileSizeLimit;} public void setFileSizeLimit (String fileSizeLimit) {this.fileSizeLimit = fileSizeLimit;}}
Note: 1, MyFileRenamePolicy a custom file renaming method.

Also taking into account the combination of cos and struts1.x defective, if you want to save the uploaded file information, you can use other methods, such as stored in the session.

Download demo: http://download.csdn.net/source/2180135

Effect diagram:

[Transfer] SWFUpload + COS + servelet upload component upload large attachments to achieve

Also google the next in line Cos, FileUpload, SmartUpload upload performance comparison, and he is testing the next Cos and struts upload, Cos that really is quite fast, especially when large attachments.

This is a test of the use of other people online 2M, 20M, 45M, 200M upload test three components the size of the time it takes (in ms):
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Transfer] SWFUpload + COS + servelet upload component upload large attachments to achieve

  • Process migration from tomcat to websphere changes

    Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm's

  • JAVA EE JSP_JNDI

    dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries