struts2+swfuplaod多文件上传

启示页面,采用官方给的页面样式

可以同时选中多个文件(不仅仅是图片)

上传后的效果也很不错

起始页代码:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.   <link href="<%=basePath%>css/default.css" rel="stylesheet" type="text/css" />   
  11.     <script type="text/javascript" src="<%=basePath%>js/swfupload.js"></script>   
  12.     <script type="text/javascript" src="<%=basePath%>js/swfupload.queue.js"></script>   
  13.     <script type="text/javascript" src="<%=basePath%>js/fileprogress.js"></script>   
  14.     <script type="text/javascript" src="<%=basePath%>js/handlers.js"></script>   
  15.     <!-- 初始化swfupload 对象-->   
  16.    <script type="text/javascript">   
  17.         var upload1, upload2;   
  18.   
  19.         window.onload = function() {   
  20.             upload1 = new SWFUpload({   
  21.                 // Backend Settings   
  22.                 upload_url: "PictureAction.action",   
  23.                 post_params: {"picSESSID" : "songhao"},   
  24.                 file_post_name: "file",   
  25.                 // File Upload Settings   
  26.                 file_size_limit : "102400"// 100MB   
  27.                 file_types : "*.*",   
  28.                 file_types_description : "All Files",   
  29.                 file_upload_limit : "10",   
  30.                 file_queue_limit : "0",   
  31.   
  32.                 // Event Handler Settings (all my handlers are in the Handler.js file)   
  33.                 file_dialog_start_handler : fileDialogStart,   
  34.                 file_queued_handler : fileQueued,   
  35.                 file_queue_error_handler : fileQueueError,   
  36.                 file_dialog_complete_handler : fileDialogComplete,   
  37.                 upload_start_handler : uploadStart,   
  38.                 upload_progress_handler : uploadProgress,   
  39.                 upload_error_handler : uploadError,   
  40.                 upload_success_handler : uploadSuccess,   
  41.                 upload_complete_handler : uploadComplete,   
  42.   
  43.                 // Button Settings   
  44.                 button_image_url : "images/XPButtonUploadText_61x22.png",   
  45.                 button_placeholder_id : "spanButtonPlaceholder1",   
  46.                 button_width: 61,   
  47.                 button_height: 22,   
  48.                    
  49.                 // Flash Settings   
  50.                 flash_url : "js/swfupload.swf",   
  51.                    
  52.   
  53.                 custom_settings : {   
  54.                     progressTarget : "fsUploadProgress1",   
  55.                     cancelButtonId : "btnCancel1"  
  56.                 },   
  57.                    
  58.                 // Debug Settings   
  59.                 debug: false  
  60.             });   
  61.            
  62.             upload2 = new SWFUpload({   
  63.                 // Backend Settings   
  64.                 upload_url: "PictureAction.action",   
  65.                 post_params: {"SESSID" : "file"},   
  66.   
  67.                 // File Upload Settings   
  68.                 file_size_limit : "200",    // 200 kb   
  69.                 file_types : "*.jpg;*.gif;*.png",   
  70.                 file_types_description : "Image Files",   
  71.                 file_upload_limit : "10",   
  72.                 file_queue_limit : "5",   
  73.   
  74.                 // Event Handler Settings (all my handlers are in the Handler.js file)   
  75.                 file_dialog_start_handler : fileDialogStart,   
  76.                 file_queued_handler : fileQueued,   
  77.                 file_queue_error_handler : fileQueueError,   
  78.                 file_dialog_complete_handler : fileDialogComplete,   
  79.                 upload_start_handler : uploadStart,   
  80.                 upload_progress_handler : uploadProgress,   
  81.                 upload_error_handler : uploadError,   
  82.                 upload_success_handler : uploadSuccess,   
  83.                 upload_complete_handler : uploadComplete,   
  84.   
  85.                 // Button Settings   
  86.                 button_image_url : "images/XPButtonUploadText_61x22.png",   
  87.                 button_placeholder_id : "spanButtonPlaceholder2",   
  88.                 button_width: 61,   
  89.                 button_height: 22,   
  90.                    
  91.                 // Flash Settings   
  92.                 flash_url : "js/swfupload.swf",   
  93.   
  94.                 swfupload_element_id : "flashUI2",      // Setting from graceful degradation plugin   
  95.                 degraded_element_id : "degradedUI2",    // Setting from graceful degradation plugin   
  96.   
  97.                 custom_settings : {   
  98.                     progressTarget : "fsUploadProgress2",   
  99.                     cancelButtonId : "btnCancel2"  
  100.                 },   
  101.   
  102.                 // Debug Settings   
  103.                 debug: false  
  104.             });   
  105.   
  106.          }   
  107.            
  108.     </script>   
  109.   </head>   
  110.      
  111.   <body>   
  112.   <div id="header">   
  113.     <h1 id="logo"><a href="../">SWFUpload</a></h1>   
  114.     <div id="version">v2.2.0</div>   
  115. </div>   
  116.   <div id="content">   
  117.     <h2>Multi-Instance Demo</h2>   
  118.     <form action="pictureAction" method="post" name="thisform" enctype="multipart/form-data">   
  119.         <p>This page demonstrates how multiple instances of SWFUpload can be loaded on the same page.   
  120.             It also demonstrates the use of the graceful degradation plugin and the queue plugin.</p>   
  121.         <table>   
  122.             <tr valign="top">   
  123.                 <td>   
  124.                     <div>   
  125.                         <div class="fieldset flash" id="fsUploadProgress1">   
  126.                             <span class="legend">Large File Upload Site</span>   
  127.                         </div>   
  128.                         <div style="padding-left: 5px;">   
  129.                             <span id="spanButtonPlaceholder1"></span>   
  130.                             <input id="btnCancel1" type="button" value="Cancel Uploads" οnclick="cancelQueue(upload1);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />   
  131.                             <br />   
  132.                         </div>   
  133.                     </div>   
  134.                 </td>   
  135.                 <td>   
  136.                     <div>   
  137.                         <div class="fieldset flash" id="fsUploadProgress2">   
  138.                             <span class="legend">Small File Upload Site</span>   
  139.                         </div>   
  140.                         <div style="padding-left: 5px;">   
  141.                             <span id="spanButtonPlaceholder2"></span>   
  142.                             <input id="btnCancel2" type="button" value="Cancel Uploads" οnclick="cancelQueue(upload2);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />   
  143.                             <br />   
  144.                         </div>   
  145.                     </div>   
  146.                 </td>   
  147.             </tr>   
  148.         </table>   
  149.     </form>   
  150.     </div>   
  151.   </body>   
  152. </html>  

upload.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <%@ taglib prefix="s" uri="/struts-tags" %>   
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  6. %>   
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  9. <html>   
  10.   <head>   
  11.       
  12.   </head>   
  13.      
  14.   <body>   
  15.         <s:form action="PictureAction.action" enctype="multipart/form-data">   
  16.             <s:file name="file"></s:file>   
  17.             <s:submit></s:submit>   
  18.         </s:form>   
  19.   </body>   
  20. </html>  

action文件

  1. package com.action;   
  2.   
  3.   
  4.   
  5. import java.io.File;   
  6. import java.io.FileInputStream;   
  7. import java.io.FileOutputStream;   
  8. import java.io.InputStream;   
  9. import java.io.OutputStream;   
  10.   
  11. import org.apache.struts2.ServletActionContext;   
  12.   
  13.   
  14. import com.model.Picture;   
  15. import com.opensymphony.xwork2.ActionSupport;   
  16.   
  17. public class PictureAction extends ActionSupport {   
  18.   
  19.     /**  
  20.      *   
  21.      */  
  22.     private static final long serialVersionUID = 1L;   
  23.        
  24.     private File file;   
  25.        
  26.     private String fileFileName;   
  27.        
  28.     private String fileContentType;   
  29.        
  30.        
  31.        
  32.     public File getFile() {   
  33.         return file;   
  34.     }   
  35.   
  36.   
  37.   
  38.     public void setFile(File file) {   
  39.         this.file = file;   
  40.     }   
  41.   
  42.   
  43.   
  44.     public String getFileFileName() {   
  45.         return fileFileName;   
  46.     }   
  47.   
  48.   
  49.   
  50.     public void setFileFileName(String fileFileName) {   
  51.         this.fileFileName = fileFileName;   
  52.     }   
  53.   
  54.   
  55.   
  56.     public String getFileContentType() {   
  57.         return fileContentType;   
  58.     }   
  59.   
  60.   
  61.   
  62.     public void setFileContentType(String fileContentType) {   
  63.         this.fileContentType = fileContentType;   
  64.     }   
  65.   
  66.   
  67.   
  68.     /**  
  69.      * 用于实现上传功能  
  70.      */  
  71.     @Override  
  72.     public String execute() throws Exception {   
  73.         // TODO Auto-generated method stub   
  74.         //实现上传   
  75.         InputStream is = new FileInputStream(file);   
  76.         String root = ServletActionContext.getRequest().getRealPath("/upload");   
  77.         System.out.println(this.getFileContentType() );   
  78.         File deskFile = new File(root,this.getFileFileName());   
  79.         OutputStream os = new FileOutputStream(deskFile);   
  80.         byte [] bytefer = new byte[400];   
  81.         int length = 0 ;    
  82.         while((length = is.read(bytefer) )>0)   
  83.         {   
  84.             os.write(bytefer,0,length);   
  85.         }   
  86.         os.close();   
  87.         is.close();   
  88.         return SUCCESS;   
  89.     }   
  90.         
  91.            
  92. }  

 

具体代码在我的资源当中有传,喜欢的朋友可以看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值