研究了半天的ExtJs+Struts2多文件上传

    技术2025-01-07  48

    前台EXT(假设上传文件为2个):主要就是个formPanel,items中写为: {      xtype : 'textfield',      fieldLabel : '上传文件1',     name : 'file',      inputType : 'file'     }, {      xtype : 'textfield',      fieldLabel : '上传文件2',     name : 'file',      inputType : 'file'     } 其他地方不详细诉说了,不明白看EXT表单提交去,别忘记fileUpload : true就可以了 注意:因为是用struts2处理,name都是一样的file struts2后台:  private List<File> file;对应前面的name // 使用列表保存多个上传文件的文件名 private List<String> fileFileName; // 使用列表保存多个上传文件的MIME类型 private List<String> fileContentType; // 保存上传文件的目录,相对于Web应用程序的根路径,在struts.xml文件中配置 private String uploadDir; get/set方法略 public void fileUpLoad() {    String newFileName = null;    BufferedOutputStream bos = null;    BufferedInputStream bis = null;    String reInfo="";//上传成功返回的东西    for (int i = 0; i < file.size(); i++) {     long now = new Date().getTime();     int index = fileFileName.get(i).lastIndexOf('.');     String path = ServletActionContext.getServletContext().getRealPath(       uploadDir);     File dir = new File(path);     if (!dir.exists())      dir.mkdir();//创建个文件夹     if (index != -1)      newFileName = fileFileName.get(i).substring(0, index) + "-"        + now + fileFileName.get(i).substring(index);//生成新文件名     else      newFileName = fileFileName.get(i) + "-" + now;     reInfo+=newFileName+"@";     bos = null;     bis = null;     try {      FileInputStream fis = new FileInputStream(file.get(i)); // /

    bis = new BufferedInputStream(fis);      FileOutputStream fos = new FileOutputStream(new File(dir,        newFileName));      bos = new BufferedOutputStream(fos);      byte[] buf = new byte[4096];      int len = -1;      while ((len = bis.read(buf)) != -1) {       bos.write(buf, 0, len);      }     } catch (Exception e) {      /错误返回      try {       HttpServletResponse response = ApplicationWebRequestContext         .getWebApplicationContext().getResponse();       String msg = "{success:false,errors:{name:'上传错误'}}";       response.getWriter().write(msg);      } catch (IOException e1) {       // TODO Auto-generated catch block       e1.printStackTrace();      }           } finally {      善后      try {       if (null != bis)        bis.close();      } catch (IOException e) {       e.printStackTrace();      }      try {       if (null != bos)        bos.close();      } catch (IOException e) {       e.printStackTrace();      }     }         }       //最后若没错误返回就这里返回了     try {     HttpServletResponse response = ApplicationWebRequestContext       .getWebApplicationContext().getResponse();     response.setCharacterEncoding("UTF-8");     response.setContentType("text/html");     String msg = "{success:true,msg:'"+reInfo+"'}";     response.getWriter().write(msg);    } catch (Exception e) {     e.printStackTrace();    } } 改方法参考了孙鑫的strut2深入详解,自己修改而成 注意上面2个黑体,尤其是第2行,不加的话浏览器会很悲剧的再你上传陈宫以后的返回前后加上<pre>,于是ext表单获得返回失败,这就是为什么有些人用其他EXT上传组件时候上传成功一直卡成进度条那

    最新回复(0)