JQuery、ajaxFileUpload、Struts2和注解异步上传文件

页面:![在此输入图片描述][1]



第一步:需要导入jquery-1.7.2.min.js、ajaxfileupload.js两个文件,在jsp页面引入的顺序必须是jquery-1.7.2.min.js在先,ajaxfileupload.js在后,因为ajaxfileupload.js依赖与jquery包,所以你懂得!



 jsp页面代码:
<!-- lang: html -->
<button id="uploadSubmit" type="submit" class="btn btn-sm btn-info col-md-1 col-md-offset-1" onclick="FileUpload('uploadSubmit');">
                    <span class="glyphicon glyphicon-upload"></span>Upload
                </button>

js代码:
<!-- lang: js -->
function FileUpload(buttonId) {
$.ajaxFileUpload({
	url : 'fileUpload!upload',// 用于文件上传的服务器端请求地址
	type : "post",
	dataType : "json",
	timeout : 1000,
	secureuri : false,// 一般设置为false
	fileElementId : uploadId,// 文件上传空间的id属性 <input type="file" id="uploadId" />
	error : function(XMLHttpRequest, textStatus, errorThrown) {
		
	},
	success : function(data) {
		
	}
	
});

}

第二步:action代码
<!-- lang: java -->
package cn.caculate.web.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import cn.caculate.service.upload.IFileUploadService;

import com.opensymphony.xwork2.ActionSupport;

@Action("fileUpload")
@InterceptorRefs(value = { @InterceptorRef("fileUploadStack") })
@Results({ @Result(name = "jsonType", type = "json") })
public class CopyOfFileUploadAction extends ActionSupport {

private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024;


/**
 * 需要上传的文件
 */
private File upload;

/**
 * 上传文件的类型
 */
private String uploadContentType;

/**
 * 文件名
 */
private String uploadFileName;

/**
 * 上传之后的文件名
 */
private String storageFileName;

/**
 * 文件上传的路径
 */
public String path = ServletActionContext.getServletContext().getRealPath(
		File.separator + "WEB-INF" + File.separator + "file");

/**
 * 新文件上传
 * 
 * @return
 */
public String upload() {
	try {
		// 将Struts2自动封装的文件名赋给要写入的文件
		storageFileName = uploadFileName;
		// 创建要写入的文件
		File storageFile = new File(path + "//" + storageFileName);
		copy(upload, storageFile);
		return "jsonType";
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

/**
 * 上传文件的主要方法
 * 
 * @param src
 * @param dst
 * @return
 */
public boolean copy(File src, File dst) {
	try {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src),
					BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			while (in.read(buffer) > 0) {
				out.write(buffer);
			}
		} finally {
			if (null != in) {
				in.close();
			}
			if (null != out) {
				out.close();
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return true;
}

public File getUpload() {
	return upload;
}

public void setUpload(File upload) {
	this.upload = upload;
}

public String getUploadContentType() {
	return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
	this.uploadContentType = uploadContentType;
}

public String getUploadFileName() {
	return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
	this.uploadFileName = uploadFileName;
}

public String getStorageFileName() {
	return storageFileName;
}

public void setStorageFileName(String storageFileName) {
	this.storageFileName = storageFileName;
}


public String getPath() {
	return path;
}

public void setPath(String path) {
	this.path = path;
}

}

转载于:https://my.oschina.net/hugohxb/blog/178972

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值