`
okwang1979
  • 浏览: 31002 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2上传文件与uploadify插件的应用

阅读更多
在QQ上有人问uploadify传递参数,就随便看了下,感觉挺漂亮的,正好做struts2项目就配置了一下。
项目用的是SSh框架
web.xml配置如下:这里配置了 spring struts2 以及log
<?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">
    <display-name>OMS</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/ApplicationContext.xml</param-value>
    </context-param>



    <jsp-config>
        <jsp-property-group>
          <url-pattern>*.jsp</url-pattern>
          <el-ignored>true</el-ignored>
        </jsp-property-group>
    </jsp-config>

    <!-- log4j configuration -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>oms.root</param-value>
    </context-param>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/classes/log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>3000</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <session-config>
        <session-timeout>180</session-timeout>
    </session-config>

    <!--
    <welcome-file-list>
        <welcome-file>jsp/login.jsp</welcome-file>
    </welcome-file-list>
    -->

</web-app>


因为没有用到spring的上传这里就只给struts的配置;
struts.xml
<!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
         "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<include file="struts-default.xml" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.enable.DynamicMethodInvocation"
		value="false" />
	<constant name= "struts.multipart.maxSize" value="1073741824" />
	<constant name="struts.action.extension" value="action,do,webwork" />
	<package name="query" namespace="/" extends="struts-default">
		<action name="uploadFile"  class="com.aopu.action.FileUploadAction">
			<result>/jsp/upload.jsp</result>
		</action>
		
		
	</package>
</struts>


这里面有个常量配置是配置上传大小限制,默认2M
<constant name= "struts.multipart.maxSize" value="1073741824" />

后台上传实现类
package com.aopu.action;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
		ServletContextAware, ServletRequestAware {
	private File doc;
	private String fileName;
	private String contentType;
	private ServletContext context;

	private HttpServletRequest request;

	public String execute() throws Exception {
		String targetDirectory = this.context.getRealPath("/upload");
		String[] names = ((MultiPartRequestWrapper)this.request).getFileNames("doc");
		File target = new File(targetDirectory, names[0]);
		FileUtils.copyFile(doc, target);
		return SUCCESS;

	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;

	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public File getDoc() {
		return doc;
	}

	public void setDoc(File doc) {
		this.doc = doc;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

}

后台全部实现完成
前台jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/swfobject.js"></script>
<script type="text/javascript" src="../js/jquery.uploadify.v2.1.0.min.js"></script>
<title>Insert title here</title>
</head>
<script type="text/javascript">
$(document).ready(function() {
	$("#uploadify").uploadify({
		'uploader'       : '../css/uploadify.swf',
		'script'         : 'uploadFile.action',
		'fileDataName'   : 'doc',
		'cancelImg'      : 'cancel.png',
		'folder'         : 'uploads',
		'queueID'        : 'fileQueue',
		'onCheck'        :  function(event,checkScript,fileQueue,folder,single){
		 var aa =checkScript;
		 	alert(checkScript);
			return false;
		 },
		'auto'           : false,
		'multi'          : true
	});

});
 function mycheck(event,checkScript,fileQueue,folder,single){
		 var aa =checkScript;
		 	alert(checkScript);
		 }

</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p> <a href="javascript:$('#uploadify').uploadifyUpload();">Upload Files</a>|<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">Cancel All Uploads</a></p>
</body>


这里需注意后台取得前台的内容是通过'fileDataName'  配置的,我起名是doc。
auto配置是否制动提交
multi配置是否允许多文件

附件是前台的js css 以及一些资源
资源:
http://www.uploadify.com/demo/
0
0
分享到:
评论

相关推荐

    Struts2结合Jquery.uploadify上传插件的应用

    下面将详细探讨Struts2结合jQuery.uploadify插件的应用。 首先,jQuery.uploadify是一个基于jQuery的文件上传插件,它可以实现异步多文件上传,并且提供了多种自定义选项以满足不同需求。它的核心功能包括预览、...

    Struts2实现Uploadify多文件上传

    Struts2是一个强大的MVC...通过以上步骤,我们可以利用Struts2和Uploadify实现多文件上传功能,为用户提供友好的上传体验,同时确保服务器端能正确处理和存储上传的文件。在实际项目中,应根据具体需求进行定制和优化。

    Uploadify结合Struts2上传demo

    // 获取上传文件 FileItem fileItem = (FileItem) request.getAttribute("file"); if (fileItem != null) { file = fileItem.getStoreLocation(); fileContentType = fileItem.getContentType(); fileFileName...

    struts2+uploadify(2.1.4)的文件上传

    Struts2是一个流行的Java web框架,它为开发者提供了模型-视图-控制器(MVC)架构,便于构建可扩展和维护的Web应用...尽管如此,对于仍然依赖Flash的环境,Struts2与Uploadify的组合仍是一种有效的文件上传解决方案。

    struts2_uploadify带进度条的多文件上传下载

    2. JSP或HTML页面:使用Uploadify插件的JavaScript代码,展示上传界面和进度条,用户可以通过此界面选择文件并触发上传。 3. 配置文件:如struts.xml,配置Struts2的动作映射和结果类型,确保上传请求能被正确路由到...

    Struts2uploadify3.1

    Uploadify插件通过JavaScript与服务器端进行交互,绕过了这些问题,使得文件上传更加顺畅。 Uploadify3.1的核心功能包括: 1. **异步上传**:Uploadify使用AJAX技术,允许用户在后台上传文件,无需刷新整个页面,...

    struts2+jquery.uploadify

    在本篇文章中,我们将深入探讨如何结合Struts2和jQuery的uploadify插件来实现文件上传功能,这在现代Web应用程序中是非常常见的需求。 首先,Struts2是一个基于MVC(Model-View-Controller)架构的开源Java框架,它...

    struts2+jquery.uploadify实现上传下载

    4. **Uploadify与Struts2集成**:在Struts2 Action中,我们需要接收上传的文件。由于Uploadify使用异步POST,所以Action需要处理Multipart请求。这通常通过继承`org.apache.struts2.views.util.multipart....

    java使用uploadify插件实现多文件上传完整demo

    本教程将详细介绍如何在Java环境中利用Uploadify插件实现这些功能。 1. **Uploadify 插件介绍** Uploadify 是一个基于jQuery的开源文件上传插件,它通过异步方式上传文件,提供用户友好的界面和多种自定义选项。...

    struts1 uploadify 多文件上传

    这里的`'script'`参数指定服务器端处理文件上传的Struts1动作,`'folder'`是上传文件的目标目录。 在Struts1中,我们需要创建一个处理文件上传的动作类。这个类通常会继承自`org.apache.struts.action.Action`,并...

    struts2 +jquey uploadify3.2 实现多文件上传,可预览、删除、排序

    1. **Uploadify插件**:Uploadify提供了丰富的配置选项,允许用户自定义上传界面、设置文件类型、大小限制等。它使用Flash或HTML5作为后端技术,实现跨浏览器的文件上传。 2. **多文件上传**:Uploadify支持选择多...

    JQuery_uploadify_struts2_jsp__ajax多文件上传

    1. `'fileDataName' : 'file'`:这是uploadify插件的一个重要参数,它定义了在服务器端如何获取上传文件的数据。如果没有设置这个属性,服务器可能无法正确识别并接收文件。例如,在Struts2的Action中,你需要通过`@...

    struts2 jquery.uploadify

    Struts2 jQuery Uploadify是一个整合了Struts2框架与jQuery Uploadify插件的示例项目,主要用于实现文件上传的功能。这个简单的demo展示了如何在Struts2应用中集成jQuery Uploadify,以便提供用户友好的、多文件异步...

    uploadify struts2实现图片上传

    本篇文章将深入探讨如何使用uploadify这款jQuery插件与Struts2框架结合,实现图片的上传功能。 首先,uploadify是一款强大的前端文件上传插件,它基于JavaScript库jQuery,提供了丰富的自定义选项和友好的用户界面...

    uploadify+struts2多文件上传实例

    `uploadify`与`Struts2`结合的多文件上传实例就是一个典型的解决方案,尤其针对跨浏览器兼容性问题,如在Firefox下的session问题。下面我们将深入探讨这两个技术以及它们如何协同工作。 `Uploadify`是一个基于...

    struts2+uploadify小李子

    Struts2提供了FileUpload拦截器,它可以处理multipart/form-data类型的表单提交,这是上传文件所必需的。在Action类中,你需要定义一个File和String类型的属性来接收上传的文件和文件名。同时,配置struts.xml文件,...

    使用jquery的uploadify插件实现文件上传

    综上所述,使用jQuery的uploadify插件实现文件上传涉及了jQuery库的使用、uploadify插件的配置、Struts2框架的集成以及Java后端的文件处理。在实际开发中,可以根据项目需求对这些步骤进行调整和优化,以实现更高效...

    Uploadify + Struts2 实现文件上传详解

    在JSP页面中,引入必要的JS和CSS文件,并设置Uploadify插件。以下是一个简单的示例: ```jsp ();%&gt; &lt;!DOCTYPE html&gt; &lt;script src="&lt;%=path%&gt;/js/jquery.js"&gt; &lt;script src="&lt;%=path%&gt;/js/swfobject.js"&gt; ...

    Struts2 结合uploadify 注解方式上传文件 带进度条显示

    这包括Struts2的核心库、文件上传插件(struts2-upload-plugin),以及Uploadify的JavaScript和CSS文件。`js`目录下的文件包含了Uploadify的JavaScript脚本和配置,`css`目录下的文件则用于样式美化。 1. **配置...

Global site tag (gtag.js) - Google Analytics