ajax多文件上传action,JQUERY AJAX+STRUTS2批量上传文件

ACTION代码

package ul;

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 java.io.PrintWriter;

import java.util.Date;

import org.apache.struts2.ServletActionContext;

import ccme.sys.base.BaseAction;

import ccme.sys.json.JSONUtil;

public class FileUploadAction extends BaseAction

{

private static final long serialVersionUID = 572146812454l ;

private static final int BUFFER_SIZE = 5 * 1024  ;

private File myFile;

private String contentType;

private String fileName;

private String imageFileName;

private String caption;

String newfilename;

String groupid;

private static void copy(File src, File dst) {

try {

InputStream in = null ;

OutputStream out = null ;

try {

FileOutputStream f=new FileOutputStream(dst);

in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);

out = new BufferedOutputStream( f, 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();

}

}

private static String getExtention(String fileName) {

int pos = fileName.lastIndexOf(".");

return fileName.substring(pos);

}

@Override

public String execute()

{

PrintWriter out = null;

FilePojo filePojo=new FilePojo();

try {

this.getResponse().setContentType("text/html;charset=utf-8");

out = this.getResponse().getWriter();

imageFileName = new Date().getTime() + getExtention(fileName);

File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages")+"/"+imageFileName);

copy(myFile, imageFile);

filePojo.setFilename(imageFileName);

filePojo.setGroupid(this.getGroupid());

filePojo.setFileszie(imageFile.length());

filePojo.setResult("true");

}

catch (Exception e)

{

e.printStackTrace();

filePojo.setResult("error");

}

out.print(JSONUtil.bean2json(filePojo));

out.close();

return null;

}

public void setMyFileContentType(String contentType) {

this .contentType = contentType;

}

public void setMyFileFileName(String fileName) {

this .fileName = fileName;

}

public void setMyFile(File myFile) {

this .myFile = myFile;

}

public String getImageFileName() {

return imageFileName;

}

public String getCaption() {

return caption;

}

public void setCaption(String caption) {

this .caption = caption;

}

public String getNewfilename() {

return newfilename;

}

public void setNewfilename(String newfilename) {

this.newfilename = newfilename;

}

public String getGroupid() {

return groupid;

}

public void setGroupid(String groupid) {

this.groupid = groupid;

}

}

struts2配置文件

JSP页面

需要:jquery.min.js,ajaxfileupload.js

var formNum=0;

function addFile()

{

formNum = formNum + 1;

var file="";

file=file+"文件:";

file=file+"
";

file=file+"";

$("#mydiv").append(file);

}

function dlefile(obj)

{

$(obj).parent().remove();

}

function uploadFile()

{

var filelist=$(":file");

for(var i=0;i

ajaxFileUpload(filelist[i]);

}

function ajaxFileUpload(myFile)

{

var groupid=myFile.id.split("_")[1];

$.ajaxFileUpload

(

{

url:'fileUpload.action',

secureuri:false,

fileElementId:myFile.id,

dataType : 'json',

data: {"newfilename": "ccc123","groupid": groupid},

success: function (data, status)

{

$("#span_"+groupid).html("上传状态:"+data.result+"|服务器文件名称:"+data.filename+"|文件大小:"+data.fileszie+"
");//remove();

},

error: function (data, status, e)

{

alert("error");

}

}

);

return false;

}

文章转载自:http://blog.sina.com.cn/s/blog_81e93ca90100yqdd.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值