juqery文件上传 plupload java修改版

最近需要用到jquery文件上传插件,发现plupload这东西挺好的,奈何后台代码是php,tomcat得配置php才能跑起来,于是稍微研究了下,改成了java代码

 

plupload的特色是

1、可以配置chunk,将一个大文件分成许多小文件上传,后台通过php合并成大文件,这里通过相应的java代码

2、实际上传的文件名是经过生成唯一的uuid,通过参数name传递到后台

3、上传文件的过程是先上传临时命名为uuid的临时文件,上传成功后会自动生成几个input标签,对应上传之后的临时文件的文件名,之后通过另一个action调用uploadFinish对临时文件进行重命名 操作或者其他操作

 

 

 

这个java代码是基于 Struts2的,不是servlet,反正都是类似的 在这基础上也容易改

 

 

public class UploadAction extends ActionSupport {


       private static final int BUFFER_SIZE = 2 * 1024;


	private File upload;
        private String name;  //plupload上传文件的临时文件名 uuid.文件后缀
	private String uploadFileName;
	private String uploadContentType;
	private int chunk;
	private int chunks;

// 。。。一堆getter setter自己生成



        private void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			if (dst.exists()) {
				out = new BufferedOutputStream(new FileOutputStream(dst, true),
						BUFFER_SIZE);  //plupload 配置了chunk的时候新上传的文件appand到文件末尾
			} else {
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
			}
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);

			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}



        public String upload() throws Exception {

		String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp")
				+ "\\" + this.getName();  // 保存目录可以自己配置 或者定义变量自行配置
		File dstFile = new File(dstPath);

		// 文件已存在删除旧文件(上传了同名的文件)
		if (chunk == 0 && dstFile.exists()) {
			dstFile.delete();
			dstFile = new File(dstPath);
		}

		copy(this.upload, dstFile);

               //System.out.println(uploadFileName + " " + uploadContentType + " "
				+ chunk + " " + chunks);
		if (chunk == chunks - 1) {
			// 一个完整的文件上传完成
		}

		return SUCCESS;
	}

	public String uploadFinish() {
		String dstPath = ServletActionContext.getServletContext().getRealPath("\\tmp");

		HttpServletRequest request = ServletActionContext.getRequest();
		
		int count = Integer.parseInt(request.getParameter("uploader_count"));
		for (int i = 0; i < count; i++) {
			uploadFileName = request.getParameter("uploader_" + i + "_name");
			name = request.getParameter("uploader_" + i + "_tmpname");
			System.out.println(uploadFileName + " " + name);
			try {

//对已经上传成功的临时文件进行操作		
                        } catch(Exception e) {
				
			}
		}
		return SUCCESS;
	}

}

补充:

从项目里抽取出了文件上传的代码,单独一个eclipse上可以跑的例子,上传上来了(注意build path里的jre路径)

 

原创,欢迎转载,请标明源

http://asyty.iteye.com/blog/1230119/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值