六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 24|回复: 0

swfupload+Struts2多文件上传

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-28 19:19:57 | 显示全部楼层 |阅读模式
公司做了一个相册模块,其中有一个要用多文件上传,可是使用的是很老的单文件上传的模式,感觉使用不是很方便,于是在网上搜索了一下,搜到了swfupload这个上传组件,于是尝试着使用struts2整合swfupload写了写,现在把代码放上来。
一、起始页代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  <link href="<%=basePath%>css/default.css" rel="stylesheet" type="text/css" />   <script type="text/javascript" src="<%=basePath%>js/swfupload.js"></script>   <script type="text/javascript" src="<%=basePath%>js/swfupload.queue.js"></script>    <script type="text/javascript" src="<%=basePath%>js/fileprogress.js"></script>    <script type="text/javascript" src="<%=basePath%>js/handlers.js"></script>   <!-- 初始化swfupload 对象-->   <script type="text/javascript">var upload1, upload2;window.onload = function() {upload1 = new SWFUpload({// Backend Settingsupload_url: "PictureAction.action",post_params: {"picSESSID" : "songhao"},file_post_name: "file",// File Upload Settingsfile_size_limit : "102400",// 100MBfile_types : "*.*",file_types_description : "All Files",file_upload_limit : "10",file_queue_limit : "0",// Event Handler Settings (all my handlers are in the Handler.js file)file_dialog_start_handler : fileDialogStart,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,// Button Settingsbutton_image_url : "images/XPButtonUploadText_61x22.png",button_placeholder_id : "spanButtonPlaceholder1",button_width: 61,button_height: 22,// Flash Settingsflash_url : "js/swfupload.swf",custom_settings : {progressTarget : "fsUploadProgress1",cancelButtonId : "btnCancel1"},// Debug Settingsdebug: false});upload2 = new SWFUpload({// Backend Settingsupload_url: "PictureAction.action",post_params: {"SESSID" : "file"},// File Upload Settingsfile_size_limit : "200",// 200 kbfile_types : "*.jpg;*.gif;*.png",file_types_description : "Image Files",file_upload_limit : "10",file_queue_limit : "5",// Event Handler Settings (all my handlers are in the Handler.js file)file_dialog_start_handler : fileDialogStart,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,// Button Settingsbutton_image_url : "images/XPButtonUploadText_61x22.png",button_placeholder_id : "spanButtonPlaceholder2",button_width: 61,button_height: 22,// Flash Settingsflash_url : "js/swfupload.swf",swfupload_element_id : "flashUI2",// Setting from graceful degradation plugindegraded_element_id : "degradedUI2",// Setting from graceful degradation plugincustom_settings : {progressTarget : "fsUploadProgress2",cancelButtonId : "btnCancel2"},// Debug Settingsdebug: false});     }    </script>  </head>    <body>  <div id="header"><h1 id="logo"><a href="../">SWFUpload</a></h1><div id="version">v2.2.0</div></div>  <div id="content"><h2>Multi-Instance Demo</h2>    <form action="pictureAction" method="post" name="thisform" enctype="multipart/form-data">    <p>This page demonstrates how multiple instances of SWFUpload can be loaded on the same page.It also demonstrates the use of the graceful degradation plugin and the queue plugin.</p><table><tr valign="top"><td><div><div class="fieldset flash" id="fsUploadProgress1"><span class="legend">Large File Upload Site</span></div><div style="padding-left: 5px;"><span id="spanButtonPlaceholder1"></span><input id="btnCancel1" type="button" value="Cancel Uploads"  disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" /><br /></div></div></td><td><div><div class="fieldset flash" id="fsUploadProgress2"><span class="legend">Small File Upload Site</span></div><div style="padding-left: 5px;"><span id="spanButtonPlaceholder2"></span><input id="btnCancel2" type="button" value="Cancel Uploads"  disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" /><br /></div></div></td></tr></table>    </form>    </div>  </body></html>
二、upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>     </head>    <body>        <s:form action="PictureAction.action" enctype="multipart/form-data">        <s:file name="file"></s:file>        <s:submit></s:submit>        </s:form>  </body></html>
三、action文件
package com.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.model.Picture;import com.opensymphony.xwork2.ActionSupport;public class PictureAction extends ActionSupport {/** *  */private static final long serialVersionUID = 1L;private File file;    private String fileFileName;        private String fileContentType;        public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}public String getFileContentType() {return fileContentType;}public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;}/** * 用于实现上传功能 */@Overridepublic String execute() throws Exception {// TODO Auto-generated method stub//实现上传InputStream is = new FileInputStream(file);String root = ServletActionContext.getRequest().getRealPath("/upload");System.out.println(this.getFileContentType() );File deskFile = new File(root,this.getFileFileName());OutputStream os = new FileOutputStream(deskFile);byte [] bytefer = new byte[400];int length = 0 ; while((length = is.read(bytefer) )>0){os.write(bytefer,0,length);}os.close();is.close();return SUCCESS;} }

上传不上去的原因的是没有新建一个文件夹 upload
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表