jquery uploadify 实现批量上传,带进度显示,判断文件大小
Uploadify是JQuery的一个上传插件,实现的效果非常不错。
支持批量上传;
带进度显示;
判断文件大小;
下面分别使用commons-fileupload 和struts2来实现一个简单的上传功能。
1,先介绍前台
首先从官网下载Uploadify,本实例使用的uploadify-v3.1
目录结构:
index.jsp页面,采用commons-fileupload方式上传
package com.struts; import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.text.DateFormat;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Random; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class ControlorAction extends ActionSupport { private File file; private String fileFileName; private String fileContentType; private String path = ""; private String errorCode = ""; String msg = ""; /** * 控制台-云存储,上传文件操作 * * @return */public void upload() {String result = "fail";HttpServletResponse response = null;try {response = ServletActionContext.getResponse(); if (null != file) {String targetDirectory = ServletActionContext.getServletContext().getRealPath("/uploads");String targetFileName = generateFileName(fileFileName);File target = new File(targetDirectory, targetFileName); if (!file.exists()) {// 处理文件大小为0kb的情况file = new File(file.getPath());FileWriter fileWriter = new FileWriter(file);fileWriter.write(" ");fileWriter.flush();fileWriter.close();} FileUtils.copyFile(file, target);result = "success";}} catch (Exception e) {e.printStackTrace();} try {response.getWriter().write(result);} catch (IOException e) {e.printStackTrace();}} /** * 生成文件名 * * @param fileName * @return */private String generateFileName(String fileName) {DateFormat format = new SimpleDateFormat("yyMMddHHmmss");String formatDate = format.format(new Date()); int random = new Random().nextInt(10000); int position = fileName.lastIndexOf(".");String extension = fileName.substring(position); return formatDate + random + extension;} 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 getFileContentType() {return fileContentType;} public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;} public String getPath() {return path;} public void setPath(String path) {this.path = path;} public String getErrorCode() {return errorCode;} public void setErrorCode(String errorCode) {this.errorCode = errorCode;} public String getMsg() {return msg;} public void setMsg(String msg) {this.msg = msg;} /** * 文件大小换上为bite * * @param s * @return */private long getFileSizeByBite(String s) {long size = 0;size = Long.parseLong(s) * 1073741824;/* * if (s.lastIndexOf("G") != -1 || s.lastIndexOf("g") != -1) { s = * s.substring(0, s.length() - 1); size = Long.parseLong(s) * * 1073741824; } else if (s.lastIndexOf("M") != -1 || s.lastIndexOf("m") * != -1) { s = s.substring(0, s.length() - 1); size = Long.parseLong(s) * * 1048576; } else if (s.lastIndexOf("T") != -1 || s.lastIndexOf("t") * != -1) { s = s.substring(0, s.length() - 1); size = Long.parseLong(s) * * 1024 * 1073741824; } */return size;} public String formetFileSize(long fileS) {// 转换文件大小DecimalFormat df = new DecimalFormat("#.00");String fileSizeString = "";if (fileS < 1024) {fileSizeString = df.format((double) fileS) + "B";} else if (fileS < 1048576) {fileSizeString = df.format((double) fileS / 1024) + "K";} else if (fileS < 1073741824) {fileSizeString = df.format((double) fileS / 1048576) + "M";} else {fileSizeString = df.format((double) fileS / 1073741824) + "G";}return fileSizeString;} }源码下载:http://pan.baidu.com/s/1oVEfk