swfupload工具及其与struts2实现文件上传

1、Index.jsp

 

  1.  

  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  3.  

  4. <%

  5.  

  6. String path = request.getContextPath();

  7.  

  8. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

  9.  

  10. %>

  11.  

  12.  

  13.  

  14. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  15.  

  16. <html>

  17.  

  18. <head>

  19.  

  20. <link href="<%=basePath%>css/default.css" rel="stylesheet" type="text/css" />

  21.  

  22. <script type="text/javascript" src="<%=basePath%>js/swfupload.js"></script>

  23.  

  24. <script type="text/javascript" src="<%=basePath%>js/swfupload.queue.js"></script>

  25.  

  26. <script type="text/javascript" src="<%=basePath%>js/fileprogress.js"></script>

  27.  

  28. <script type="text/javascript" src="<%=basePath%>js/handlers.js"></script>

  29.  

  30. <!-- 初始化swfupload 对象-->

  31.  

  32. <script type="text/javascript">

  33.  

  34. var upload1;

  35.  

  36.  

  37.  

  38. window.onload = function() {

  39.  

  40. upload1 = new SWFUpload({

  41.  

  42.  

  43.  

  44. //提交路径

  45.  

  46. upload_url: "upload.action",

  47.  

  48. //向后台传递额外的参数

  49.  

  50. post_params: {"name" : "kaobian"},

  51.  

  52. //上传文件的名称

  53.  

  54. file_post_name: "file",

  55.  

  56.  

  57.  

  58. // 下面自己按照字面意思理解

  59.  

  60. file_size_limit : "102400", // 100MB

  61.  

  62. file_types : "*.*",

  63.  

  64. file_types_description : "All Files",

  65.  

  66. file_upload_limit : "10",

  67.  

  68. file_queue_limit : "0",

  69.  

  70.  

  71.  

  72. // 事件处理

  73.  

  74. file_dialog_start_handler : fileDialogStart,

  75.  

  76. file_queued_handler : fileQueued,

  77.  

  78. file_queue_error_handler : fileQueueError,

  79.  

  80. file_dialog_complete_handler : fileDialogComplete,

  81.  

  82. upload_start_handler : uploadStart,

  83.  

  84. upload_progress_handler : uploadProgress,

  85.  

  86. upload_error_handler : uploadError,

  87.  

  88. upload_success_handler : uploadSuccess,

  89.  

  90. upload_complete_handler : uploadComplete,

  91.  

  92.  

  93.  

  94. // 按钮的处理

  95.  

  96. button_image_url : "images/XPButtonUploadText_61x22.png",

  97.  

  98. button_placeholder_id : "spanButtonPlaceholder1",

  99.  

  100. button_width: 61,

  101.  

  102. button_height: 22,

  103.  

  104.  

  105.  

  106. // Flash Settings

  107.  

  108. flash_url : "js/swfupload.swf",

  109.  

  110.  

  111.  

  112.  

  113.  

  114. custom_settings : {

  115.  

  116. progressTarget : "fsUploadProgress1",

  117.  

  118. cancelButtonId : "btnCancel1"

  119.  

  120. },

  121.  

  122.  

  123.  

  124. // Debug Settings

  125.  

  126. debug: false

  127.  

  128. });

  129.  

  130. }

  131.  

  132.  

  133.  

  134. </script>

  135.  

  136. </head>

  137.  

  138.  

  139.  

  140. <body>

  141.  

  142. </div>

  143.  

  144. <div id="content">

  145.  

  146. <form action="upload.action" method="post" name="thisform" enctype="multipart/form-data">

  147.  

  148.  

  149.  

  150. <table>

  151.  

  152. <tr valign="top">

  153.  

  154. <td>

  155.  

  156. <div>

  157.  

  158.  

  159.  

  160. <div style="padding-left: 5px;">

  161.  

  162.  

  163.  

  164. <span id="spanButtonPlaceholder1"></span>

  165.  

  166. <!--<input type="button" value="上传" onclick="upload1.addPostParam('idname',encodeURI(document.getElementById('myFileName').value));upload1.startUpload();"/>

  167.  

  168. --><input id="btnCancel1" type="button" value="Cancel Uploads" onclick="cancelQueue(upload1);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />

  169.  

  170. <br />

  171.  

  172. </div>

  173.  

  174. <div class="fieldset flash" id="fsUploadProgress1">

  175.  

  176. <span class="legend">文件上传</span>

  177.  

  178. </div>

  179.  

  180. </div>

  181.  

  182. </td>

  183.  

  184. </tr>

  185.  

  186. </table>

  187.  

  188. </form>

  189.  

  190. </div>

  191.  

  192. </body>

  193.  

  194. </html>

<%@ 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({

				//提交路径
				upload_url: "upload.action",
				//向后台传递额外的参数
				post_params: {"name" : "kaobian"},
				//上传文件的名称
				file_post_name: "file",
				
				// 下面自己按照字面意思理解
				file_size_limit : "102400",	// 100MB
				file_types : "*.*",
				file_types_description : "All Files",
				file_upload_limit : "10",
				file_queue_limit : "0",

				// 事件处理
				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_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>
  <div id="content">
    <form action="upload.action" method="post" name="thisform" enctype="multipart/form-data">
    	
		<table>
			<tr valign="top">
				<td>
					<div>
						
						<div style="padding-left: 5px;">
							
							<span id="spanButtonPlaceholder1"></span>
							<!--<input type="button" value="上传" onclick="upload1.addPostParam('idname',encodeURI(document.getElementById('myFileName').value));upload1.startUpload();"/>
							--><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 class="fieldset flash" id="fsUploadProgress1">
							<span class="legend">文件上传</span>
						</div>
					</div>
				</td>
			</tr>
		</table>
    </form>
    </div>
  </body>
</html>

Upload.action

 

 

Java代码

 

  1.  

  2. package com.action;

  3.  

  4.  

  5.  

  6. import java.io.File;

  7.  

  8. import java.io.FileInputStream;

  9.  

  10. import java.io.FileOutputStream;

  11.  

  12. import java.io.InputStream;

  13.  

  14. import java.io.OutputStream;

  15.  

  16.  

  17.  

  18. import org.apache.struts2.ServletActionContext;

  19.  

  20. import com.opensymphony.xwork2.ActionSupport;

  21.  

  22.  

  23.  

  24. public class FileUploadAction extends ActionSupport {

  25.  

  26. private File file;

  27.  

  28. private String fileFileName;

  29.  

  30. private String fileContentType;

  31.  

  32. private String savePath;

  33.  

  34.  

  35.  

  36. public String execute() throws Exception {

  37.  

  38.  

  39.  

  40. InputStream is = new FileInputStream(file);

  41.  

  42. String root = getSavePath();

  43.  

  44. //String tempName = System.currentTimeMillis()+this.getFileFileName().substring(this.getFileFileName().indexOf("."));

  45.  

  46. File deskFile = new File(root, this.getFileFileName());

  47.  

  48. OutputStream os = new FileOutputStream(deskFile);

  49.  

  50. byte[] bytefer = new byte[1024];

  51.  

  52. int length = 0;

  53.  

  54. while ((length = is.read(bytefer)) != -1) {

  55.  

  56. os.write(bytefer, 0, length);

  57.  

  58. }

  59.  

  60. os.close();

  61.  

  62. is.close();

  63.  

  64. return "success";

  65.  

  66. }

  67.  

  68.  

  69.  

  70.  

  71.  

  72. public String getSavePath() {

  73.  

  74. return ServletActionContext.getServletContext().getRealPath(savePath);

  75.  

  76. }

  77.  

  78.  

  79.  

  80.  

  81.  

  82. public void setSavePath(String savePath) {

  83.  

  84. this.savePath = savePath;

  85.  

  86. }

  87.  

  88.  

  89.  

  90.  

  91.  

  92. public File getFile() {

  93.  

  94. return file;

  95.  

  96. }

  97.  

  98.  

  99.  

  100. public void setFile(File file) {

  101.  

  102. this.file = file;

  103.  

  104. }

  105.  

  106.  

  107.  

  108. public String getFileFileName() {

  109.  

  110. return fileFileName;

  111.  

  112. }

  113.  

  114.  

  115.  

  116. public void setFileFileName(String fileFileName) {

  117.  

  118. this.fileFileName = fileFileName;

  119.  

  120. }

  121.  

  122.  

  123.  

  124. public String getFileContentType() {

  125.  

  126. return fileContentType;

  127.  

  128. }

  129.  

  130.  

  131.  

  132. public void setFileContentType(String fileContentType) {

  133.  

  134. this.fileContentType = fileContentType;

  135.  

  136. }

  137.  

  138.  

  139.  

  140. }

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.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String savePath;

	public String execute() throws Exception {
		
		InputStream is = new FileInputStream(file);
		String root = getSavePath();
		//String tempName = System.currentTimeMillis()+this.getFileFileName().substring(this.getFileFileName().indexOf("."));	
		File deskFile = new File(root, this.getFileFileName());
		OutputStream os = new FileOutputStream(deskFile);
		byte[] bytefer = new byte[1024];
		int length = 0;
		while ((length = is.read(bytefer)) != -1) {
			os.write(bytefer, 0, length);
		}
		os.close();
		is.close();
		return "success";
	}

	
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}


	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;
	}

}

Web.xml

 

 

Java代码

 

  1.  

  2. <?xml version="1.0" encoding="UTF-8"?>

  3.  

  4. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

  5.  

  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  7.  

  8. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  9.  

  10. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  11.  

  12. <welcome-file-list>

  13.  

  14. <welcome-file>index.jsp</welcome-file>

  15.  

  16. </welcome-file-list>

  17.  

  18. <filter>

  19.  

  20. <filter-name>struts2</filter-name>

  21.  

  22. <filter-class>

  23.  

  24. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  25.  

  26. </filter>

  27.  

  28. <filter-mapping>

  29.  

  30. <filter-name>struts2</filter-name>

  31.  

  32. <url-pattern>/*</url-pattern>

  33.  

  34. </filter-mapping>

  35.  

  36. </web-app>

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

Struts.xml

 

 

Java代码

 

  1.  

  2. <?xml version="1.0" encoding="UTF-8" ?>

  3.  

  4. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

  5.  

  6. <struts>

  7.  

  8.  

  9.  

  10. <!-- 配置struts2.1.8 上传是文件的最大限制为100M -->

  11.  

  12. <constant name="struts.multipart.maxSize" value="104857600" />

  13.  

  14.  

  15.  

  16. <!-- 设置struts2 上传文件时 保存的临时目录 -->

  17.  

  18. <constant name="struts.multipart.saveDir" value="C:\temp"></constant>

  19.  

  20.  

  21.  

  22. <package name="struts2" extends="struts-default">

  23.  

  24. <action name="upload" class="com.action.FileUploadAction">

  25.  

  26. <param name="savePath">/upload</param>

  27.  

  28. <result name="success">/index.jsp</result>

  29.  

  30. <result name="input">/index.jsp</result>

  31.  

  32. </action>

  33.  

  34. </package>

  35.  

  36. </struts>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

	<!-- 配置struts2.1.8 上传是文件的最大限制为100M -->
	<constant name="struts.multipart.maxSize" value="104857600" />
	
	<!-- 设置struts2 上传文件时  保存的临时目录 -->
	<constant name="struts.multipart.saveDir" value="C:\temp"></constant>
	
	<package name="struts2" extends="struts-default">
		<action name="upload" class="com.action.FileUploadAction">
			<param name="savePath">/upload</param>
			<result name="success">/index.jsp</result>
			<result name="input">/index.jsp</result>
		</action>
	</package>
</struts>
posted @ 2015-04-13 16:25  凌霄寒  阅读(176)  评论(0)    收藏  举报