server端採用struts2来处理文件上传。

所需环境:

jquery.js

ajaxfileupload.js

struts2所依赖的jar包

及struts2-json-plugin-2.1.8.1.jar

编写文件上传的Action

package com.ajaxfile.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class FileAction extends ActionSupport { private File file;
private String fileFileName;
private String fileFileContentType; private String message = "你已成功上传文件"; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} 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 getFileFileContentType() {
return fileFileContentType;
} public void setFileFileContentType(String fileFileContentType) {
this.fileFileContentType = fileFileContentType;
} @SuppressWarnings("deprecation")
@Override
public String execute() throws Exception { String path = ServletActionContext.getRequest().getRealPath("/upload"); try {
File f = this.getFile();
if(this.getFileFileName().endsWith(".exe")){
message="对不起,你上传的文件格式不同意!!!";
return ERROR;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = "对不起,文件上传失败了!!!!";
}
return SUCCESS;
} }

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" extends="json-default">
<action name="fileUploadAction" class="com.ajaxfile.action.FileAction">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
</result>
</action>
</package>
</struts>

注意结合Action观察struts.xml中result的配置。  contentType參数是一定要有的,否则浏览器总是提示将返回的JSON结果另存为文件,不会交给ajaxfileupload处理。这是因 为struts2 JSON Plugin默认的contentType为application/json。而ajaxfileupload则要求为text/html。 

文件上传的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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload()
{ $("#loading")
.ajaxStart(function(){
$(this).show();
})//開始上传文件时显示一个图片
.ajaxComplete(function(){
$(this).hide();
});//文件上传完毕将图片隐藏起来 $.ajaxFileUpload
(
{
url:'fileUploadAction.action',//用于文件上传的server端请求地址
secureuri:false,//一般设置为false
fileElementId:'file',//文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: 'json',//返回值类型 一般设置为json
success: function (data, status) //server成功响应处理函数
{
alert(data.message);//从server返回的json中取出message中的数据,当中message为在struts2中action中定义的成员变量 if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)//server响应失败处理函数
{
alert(e);
}
}
) return false; }
</script>
</head>
<body>
<img src="loading.gif" id="loading" style="display: none;">
<input type="file" id="file" name="file" />
<br />
<input type="button" value="上传" onclick="return ajaxFileUpload();">
</body>
</html>

普通的异步提交

$.ajax({  

          url:'delpicture.html',  

          type:'post',  

          data: 'proId='+proId+'&path='+path, 

          dataType:'json',  

          success:function (data) {  

         alert(data.message);

         window.location.reload();

          },

          error:function (data) {  

         alert(data.message);

         window.location.reload();

          } 

      });

这里的success和error中必须和以下配置的一样

看我的配置,同一个class 用不同的action的name 去调用FileAction中不同的方法

<package name="struts2" extends="json-default">

        <action name="fileUploadAction" class="com.xxx.json.FileAction">

            <result type="json" name="success">

                <param name="contentType">

                    text/html

                </param>

            </result>

            <result type="json" name="error">

                <param name="contentType">

                    text/html

                </param>

            </result>

        </action>

        

         <action name="delpicture" class="com.xxx.json.FileAction" method="delpicture">  

            <result type="json" name="success">

                <param name="contentType">

                    text/html

                </param>

            </result>

            <result type="json" name="error">

                <param name="contentType">

                    text/html

                </param>

            </result>

        </action>  

 </package>

Struts2中的异步提交(ajaxfileupload异步上传(图片)插件的使用)的更多相关文章

  1. ajaxFileUpload onchang上传文件插件第二次失效刷新一次才能再次调用触发change事件

    关于用ajaxfileupload时,遇到一个要刷新一次页面才能再次上传, ajaxFileUpload 用onchang上传只能上传一次 第二次就失效了 我找这个问题找了很长时间 ajaxFileU ...

  2. 利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载

    利用struts2进行单个文件,批量文件上传,ajax异步上传以及下载 1.页面显示代码 <%@ page language="java" import="java ...

  3. JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传

    Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...

  4. 适用于各浏览器支持图片预览,无刷新异步上传js插件

    文件上传无疑是web应用中一个非常常用的功能,不管是PHP.jsp还是aspx.mvc等都会需要文件上传,但是众所周知当使用自带的文件上传功能时总会出现页面刷新的情况.当然现在有了html5这个好东西 ...

  5. PHP+ajaxForm异步带进度条上传文件实例

    在使用ajaxForm方法之前,首先需要安装form.js的插件,网上有: 一.首先说用法,ajaxForm可以接收0或1个参数,该参数可以是一个变量.一个对象或回调函数,这个对象主要有以下参数: v ...

  6. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  7. jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能

     Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...

  8. 基于 Struts2 的单文件和多文件上传

    文件的上传下载是 Web 开发中老生常谈的功能,基于 Struts2 框架对于实现这一功能,更是能够给我们带来很多的便利.Struts2 已经有默认的 upload 拦截器.我们只需要写参数,它就会自 ...

  9. MVC+AjaxFileUpload文件上传

    来源:微信公众号CodeL 本次给大家分享的是ajaxfileupload文件上传插件,百度一大堆功能超炫的文件上传插件,为什么我们会选择这个插件呢? 原因是在此之前,我们尝试使用过很多基于flash ...

随机推荐

  1. linux下jenkins安装

    在安装jenkins之前.首先确认jdk和tomcat,maven已经配置好 详细配置方法,请看的我博客. jdk:jdk的安装与配置 tomcat:tomcat的安装与配置 maven:maven的 ...

  2. hdu4870 Rating (高斯消元或者dp)

    Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. hadoop无法启动DataNode问题

    因为种种原因,今天又一次安装hadoop集群.清空了/tmp下的文件夹,重新启动集群,hadoop namenode -format 之后  start-all   可是没有发现DataNode的守护 ...

  4. n个整数全排列的递归实现(C++)

    全排列是很经常使用的一个小算法,以下是n个整数全排列的递归实现,使用的是C++ #include <iostream> using namespace std; int n = 0; vo ...

  5. [iOS翻译]《iOS7 by Tutorials》在Xcode 5里使用单元測试(上)

    简单介绍: 单元測试是软件开发的一个重要方面.毕竟,单元測试能够帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元測试不是万能的,但Apple把它作为开发工具包的 ...

  6. 全面具体介绍一个P2P网贷领域的ERP系统的主要功能

        一般的P2P系统,至少包含PC站点的前端和后端.前端系统的功能.能够參考"P2P系统哪家强,功能事实上都一样" http://blog.csdn.net/fansunion ...

  7. 赵雅智_android获取本机运营商,手机号部分能获取

    手机号码不是全部的都能获取.仅仅是有一部分能够拿到. 这个是因为移动运营商没有把手机号码的数据写入到sim卡中.SIM卡仅仅有唯一的编号.供网络与设备 识别那就是IMSI号码,手机的信号也能够说是通过 ...

  8. Redis通过命令行进行配置

    redis 127.0.0.1:6379[1]> config set requirepass my_redis OK redis 127.0.0.1:6379[1]> config ge ...

  9. Pocket英语语法---六、感官动词接不同的动词表示什么意思

    Pocket英语语法---六.感官动词接不同的动词表示什么意思 一.总结 一句话总结:其实进行时一般是表示连续,动词原形一般表示常态,过去分词一般表示被动(或者完成). 感官动词接原型表示动作的一般情 ...

  10. webservice为什么不能用List参数,而只能用数组代替,我想是否因为List没有具体的类型信息,但用泛型的List(如:List<customer>)为什么也不行。如果用作参数的类中含有List<T>字段该如何处理?webservice参数是否支持

    转自:https://social.microsoft.com/Forums/zh-CN/aded4301-b5f1-4aa6-aa46-16c46a60d05e/webservice20026201 ...