struts2+swfupload多文件上传

上传页面代码
swfupload参数网上都有,可以查一下

upload_url: "uploadAction.action"//上传文件所请求的action

file_post_name:接收文件的aciton属性
在action中还要加上*FileName,*就是file_post_name你所设置的,然后设置setget方法。

button开头的是设置按钮属性

uploadsuccess等是设置回调,都在handler.js中.


<%@ 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;

window.onload = function() {
upload1 = new SWFUpload({
// Backend Settings
upload_url: "uploadAction.action",
post_params: {"picSESSID" : "songhao"},
file_post_name: "fileData",
// File Upload Settings
file_size_limit : "102400", // 100MB
file_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 Settings
button_image_url : "images/XPButtonUploadText_61x22.png",
button_placeholder_id : "spanButtonPlaceholder1",
button_width: 61,
button_height: 22,

// Flash Settings
flash_url : "js/swfupload.swf",


custom_settings : {
progressTarget : "fsUploadProgress1",
cancelButtonId : "btnCancel1"
},

// Debug Settings
debug: false
});
     }
   
</script>
  </head>
 
  <body>

  <div id="content">
<h2>File Upload</h2>
    <form action="" method="post" name="thisform" enctype="multipart/form-data">
    <p>文件上传</p>
<table>
<tr valign="top">
<td>
<div>
<div class="fieldset flash" id="fsUploadProgress1">
<span class="legend">上传文件列表</span>
</div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder1"></span>
<input id="btnCancel1" type="button" value="Cancel Uploads" onclick="cancelQueue(upload1);"

disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</div>
</div>
</td>
</tr>
</table>
    </form>
    </div>
  </body>
</html>


action代码
package com.xpec.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Form;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xpec.enums.UploadState;

public class FileUpLoadAction extends ActionSupport{

/**
*
*/
private static final long serialVersionUID = 1L;

private File fileData;

    private String fileDataFileName;
   
    private String fileDataContentType;

public String getFileDataContentType() {
return fileDataContentType;
}



public void setFileDataContentType(String fileDataContentType) {
this.fileDataContentType = fileDataContentType;
}



public File getFileData() {
return fileData;
}



public void setFileData(File fileData) {
this.fileData = fileData;
}



public String getFileDataFileName() {
return fileDataFileName;
}



public void setFileDataFileName(String fileDataFileName) {
this.fileDataFileName = fileDataFileName;
}

public String fileUpLoad() throws IOException{
InputStream is = new FileInputStream(fileData);
File deskFile = new File("D:/upLoadFile/fileUp",this.getFileDataFileName());
OutputStream os = new FileOutputStream(deskFile);
byte [] bytefer = new byte[1024];
int length = 0 ;
while((length = is.read(bytefer) )>0)
{
os.write(bytefer,0,length);
}
os.close();
is.close();
return "success";
}
}


struts.xml
<action name="uploadAction" class="com.xpec.action.FileUpLoadAction" method="fileUpLoad">
<result name="success">/index.jsp</result>
</action>
上传成功之后会找寻index.jsp也就是你配置的,不回调转但是会找,会跳到handler.js里的uploadsuccess,serverdata就是index.jsp内容你需要用正则表达式取出相应的数据来

判断是否上传成功,弹出相应提示。

fileupload.jsp,fileuploadaction.action, index.jsp这个三文件是上传代码。其他的只是随意的测试。[size=large]
[/size]

猜你喜欢

转载自longhahe.iteye.com/blog/1756339
今日推荐