UploadDialog使用完整代码

来源:互联网 发布:smartptt对讲软件下载 编辑:程序博客网 时间:2024/06/06 03:37

原文:http://blog.csdn.net/primary_wind/article/details/24592305


1.导入uploaddialog需要的js库

附上下载地址,里面有所用到的方法的介绍

http://www.max-bazhenov.com/dev/upload-dialog-2.0/

1.首先创建dialog对象

[javascript] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. var uploadDialog = Ext.create("Ext.ux.UploadDialog.Dialog", {  
[javascript] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. url:"uploadexl.do"//处理请求servlet  
  2. shadow : true,  
  3. post_var_name:"uploadFiles",   
  4. draggable:true,   
  5. resizable:true,   
  6. constraintoviewport:true,   
  7. modal:true,   
  8. reset_on_hide:false,   
  9. allow_close_on_upload:false,   
  10. upload_autostart:false,   
  11. base_params:{},   
  12. post_var_name:"file",   
  13. permitted_extensions:["xls"], //允许上传文件后缀  
  14. );  
2.编写各种状态响应的操作方法,并注册

[javascript] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. onUploadSuccess = function (dialog, filename, resp_data, record) {  
  2.         Ext.Msg.alert("提示","文件已经上传完毕!");  
  3.         store.load();  
  4.     };  
  5.       
  6. onUploadFailed = function (dialog, filename, resp_data, record) {  
  7.         Ext.Msg.alert(resp_data.data);  
  8.     };  
  9.       
  10. onUploadComplete = function (dialog) {  
  11.         Ext.Msg.alert("提示","文件已经上传完毕!");  
  12.         uploadDialog.hide();  
  13.         store.load();  
  14.     };  
  15.       
  16. uploadDialog.on("uploadsuccess", onUploadSuccess);  
  17. uploadDialog.on("uploadfailed", onUploadFailed);  
  18. uploadDialog.on("uploaderror", onUploadFailed);  
  19. uploadDialog.on("uploadcomplete", onUploadComplete);  


3.编写uploadservlet,需要用到另外的jar包进行上传文件的读取,可以搜索org.apache.commons.fileupload进行下载导入即可。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package view;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.   
  8. import javax.servlet.ServletConfig;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.http.HttpServlet;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13.   
  14. import org.apache.commons.fileupload.FileItem;  
  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  17.   
  18. import data.UserManagerDAO;  
  19.   
  20. public class UploadServlet extends HttpServlet {  
  21.     private static final long serialVersionUID = 1L;  
  22.     public static String uploadPath = "";  
  23.   
  24.     public void init(ServletConfig config) throws ServletException {  
  25.         uploadPath = config.getServletContext().getRealPath("/") + "upload/";  
  26.         super.init(config);  
  27.     }  
  28.   
  29.     @SuppressWarnings("deprecation")  
  30.     protected void doPost(HttpServletRequest request,  
  31.             HttpServletResponse response) throws ServletException, IOException {  
  32.         request.setCharacterEncoding("utf-8");  
  33.         response.setContentType("text/html;charset=UTF-8");  
  34.         try {  
  35.             DiskFileItemFactory factory = new DiskFileItemFactory();  
  36.             factory.setSizeThreshold(4096);  
  37.             File path = new File(uploadPath);  
  38.             if (!path.exists()) {  
  39.                 path.mkdir();  
  40.             }  
  41.             factory.setRepository(path);  
  42.   
  43.             ServletFileUpload upload = new ServletFileUpload(factory);  
  44.             upload.setSizeMax(2147483647L);  
  45.             List<FileItem> fileItems = null;  
  46.             fileItems = upload.parseRequest(request);  
  47.             Iterator<FileItem> i = fileItems.iterator();  
  48.             while (i.hasNext()) {  
  49.                 FileItem fi = (FileItem) i.next();  
  50.                 String fileName = fi.getName();  
  51.                 int start = fileName.lastIndexOf("\\");  
  52.                 String paths = fileName.substring(start + 1, fileName.length());  
  53.                 System.out.println(fileName);  
  54.                 fi.write(new File(uploadPath + paths));  
  55.   
  56.             }  
  57.             File file = new File(uploadPath);  
  58.             uploadPath = uploadPath.replaceAll("/""\\\\");  
  59.             String test[];  
  60.             test = file.list();  
  61.             for (int b = 0; b < test.length; b++) {  
  62.                 String getfilename = uploadPath + test[b];  
  63.                 getfilename = getfilename.replaceAll("\\\\", "/");  
  64.                 if (getfilename.endsWith(".xls")) {  
  65.                     UserManagerDAO.saveExcel(getfilename);  
  66.                 }  
  67.             }  
  68.             response.getWriter().write("{success:true,message:'文件上传成功'}");  
  69.         } catch (Exception e) {  
  70.             System.out.println(e.getMessage() + " : " + e.getCause());  
  71.             response.getWriter().write("{success:false,message:'error...'}");  
  72.         }  
  73.     }  
  74. }  


4.完毕!
0 0
原创粉丝点击