1.导入uploaddialog需要的js库
附上下载地址,里面有所用到的方法的介绍
http://www.max-bazhenov.com/dev/upload-dialog-2.0/
1.首先创建dialog对象
var uploadDialog = Ext.create("Ext.ux.UploadDialog.Dialog", {
url:"uploadexl.do", //处理请求servlet
shadow : true,
post_var_name:"uploadFiles",
draggable:true,
resizable:true,
constraintoviewport:true,
modal:true,
reset_on_hide:false,
allow_close_on_upload:false,
upload_autostart:false,
base_params:{},
post_var_name:"file",
permitted_extensions:["xls"], //允许上传文件后缀
});
2.编写各种状态响应的操作方法,并注册
onUploadSuccess = function (dialog, filename, resp_data, record) {
Ext.Msg.alert("提示","文件已经上传完毕!");
store.load();
};
onUploadFailed = function (dialog, filename, resp_data, record) {
Ext.Msg.alert(resp_data.data);
};
onUploadComplete = function (dialog) {
Ext.Msg.alert("提示","文件已经上传完毕!");
uploadDialog.hide();
store.load();
};
uploadDialog.on("uploadsuccess", onUploadSuccess);
uploadDialog.on("uploadfailed", onUploadFailed);
uploadDialog.on("uploaderror", onUploadFailed);
uploadDialog.on("uploadcomplete", onUploadComplete);
package view;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import data.UserManagerDAO;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String uploadPath = "";
public void init(ServletConfig config) throws ServletException {
uploadPath = config.getServletContext().getRealPath("/") + "upload/";
super.init(config);
}
@SuppressWarnings("deprecation")
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
File path = new File(uploadPath);
if (!path.exists()) {
path.mkdir();
}
factory.setRepository(path);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(2147483647L);
List<FileItem> fileItems = null;
fileItems = upload.parseRequest(request);
Iterator<FileItem> i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
int start = fileName.lastIndexOf("\\");
String paths = fileName.substring(start + 1, fileName.length());
System.out.println(fileName);
fi.write(new File(uploadPath + paths));
}
File file = new File(uploadPath);
uploadPath = uploadPath.replaceAll("/", "\\\\");
String test[];
test = file.list();
for (int b = 0; b < test.length; b++) {
String getfilename = uploadPath + test[b];
getfilename = getfilename.replaceAll("\\\\", "/");
if (getfilename.endsWith(".xls")) {
UserManagerDAO.saveExcel(getfilename);
}
}
response.getWriter().write("{success:true,message:'文件上传成功'}");
} catch (Exception e) {
System.out.println(e.getMessage() + " : " + e.getCause());
response.getWriter().write("{success:false,message:'error...'}");
}
}
}