Ext 文件上传

http://hi.baidu.com/%B1%F9%C3%CE%CE%DE%BA%DB/blog/item/3b19542954c90ff799250a29.html

前台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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值