[Transfer] SWFUpload + COS + servelet upload component upload large attachments to achieve
Advertisements
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?
- <% @ 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="css/default.css" mce_href="css/default.css" rel="stylesheet" type="text/css" />
- <mce:script type="text/javascript" src="js/swfupload/swfupload.js" mce_src="js/swfupload/swfupload.js"> </ mce: script>
- <mce:script type="text/javascript" src="js/swfupload/swfupload.queue.js" mce_src="js/swfupload/swfupload.queue.js"> </ mce: script>
- <mce:script type="text/javascript" src="js/swfupload/fileprogress.js" mce_src="js/swfupload/fileprogress.js"> </ mce: script>
- <mce:script type="text/javascript" src="js/swfupload/handlers.js" mce_src="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>
<% @ 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?
- <? 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>
<? 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?
- 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;
- }
- }
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:
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):
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 ...












