[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):

分类:Java 时间:2010-11-30 人气:173
分享到:
blog comments powered by Disqus

相关文章

  • JSP file referenced in the JavaScript file path 2010-07-26

    This morning, wanted to do a query to support click on the date the page, the date of JS file by clicking the page you want to control the introduction of means to achieve, try a number of shows JS can not properly load the calendar control, the orig

  • I work in the application system is jsp, the large number of javascript program, once the "automation server can not create object" problem, a large number of reports and inquiries can not be saved, think hard. Thousands of taste million 2011-05-20

    I work in the application system is jsp, the large number of javascript program, once the "automation server can not create object" problem, a large number of reports and inquiries can not be saved, think hard, try thousand million test, and fin

  • The script type jquery ajax request 2010-11-27

    Little things used to be a recent jquery's ajax Obtain data from outside the station because that is the address http://hq.sinajs.cn/list=sz002004 (no way to feel a little bit of Winbond Pharmaceuticals - -!) First, use the gb2312 encoding sina not s

  • 变量存取范围及JSP.JSTL和Struts技术中变量的存取方法 2014-10-15

    JSP中变量的定义范围实际有5种:本地范围,页面范围(page),请求范围(request),会话范围(session)和应用程序范围(application). 以下介绍在各种技术中的变量存取方法. u JSP中: 存储: 本地范围变量直接定义,如: String name="name in page"; page范围变量保存在pageContext隐式对象中,如: pageContext.setAttribute("nameContext","name

  • JSP dynamic display time using JavaScript 2010-08-24

    The need to display the location of insertion: <div> </ Div> <script> setInterval ("time.innerHTML = new Date (). toLocaleString () + 'week' + 'Day 123 456'. charAt (new Date (). getDay ());", 1000); </ Script> Showing re

  • 纯JSP+DWR实现三级联动下拉选择菜单实现技巧 2015-04-04

    今天我做了一个dwr+jsp做的例子:纯JSP+DWR实现三级联动下拉选择菜单,感兴趣的朋友可以参考下,或许本文对你有所帮助 网上看到一些例子,对于一个简单的三级联动,都加上什么Struts, Hibernate诸如此类的框架.这个Ajax联动殊不知和这些框架有什么关系,一个小Demo干嘛整得那么大. 今天我做了一个dwr+jsp做的例子. web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-a

  • Use Jquery, Ajax + Struts + Spring + Ibatis write an infinite-level tree, for your reference 2010-07-14

    Look at the jsp page, tree.jsp, Code as follows: <%@ page contentType="text/html;charset=UTF-8" %> <% String context = request.getContextPath(); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <h

  • Hibernate + Struts practice 2010-08-01

    Hastily finished study Struts, is now learning hibernate. In order to consolidate what they have learned about, looking for a simple version of the "Olympic ticket ordering system" to do exercises. In the process of recording what the whole exer

  • jsp call camera camera, control the camera operation 2010-12-02

    Add link: http://peihexian.javaeye.com/blog/676246 1. Foreground JSP page code <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01

  • JSP FusionCharts Free显示图表 具体实现 2014-05-28

    这篇文章介绍了JSP FusionCharts Free显示图表的具体实现,有需要的朋友可以参考一下 FusionCharts Free的下载地址:http://www.fusioncharts.com/goodies/fusioncharts-free/ 接下来在jsp页面中引入js <script type="text/javascript" src="FusionChartsFree/JSClass/FusionCharts.js"></sc

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.665 (s). 10 q(s)