9910

单飞

   :: 首页 :: 联系 :: 聚合  :: 管理
// 创建一个FormPanel组件实例
    var loginForm = new Ext.FormPanel({
                    id:'loginForm',// formPanel组件的ID
                    width:600,// 组件宽度
                    height:300,// 组件高度
                    frame:true,
                    fileUpload: true,
                    enctype:'multipart/form-data',
                    //实现非AJAX提交表单一定要加下面的两行!
                    onSubmit: Ext.emptyFn,
                    method:'POST',
                    align:'center',// 组件居左布局,还有right和center两个值可以选择
                    name: 'loginForm', //组件名称
                    labelAlign:"left",//让label居右
                    labelWidth:120,//定义label的宽度
                    items:[{

xtype: "textfield",
                            inputType:'file',
                            name: 'processFile',
                            fieldLabel: '文件',
                            allowBlank:false,
                            anchor:'95%'


public void deploy(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setCharacterEncoding("utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");  
        String tmpDir = getServletContext().getRealPath("/temp");// 初始化上传文件的临时寄放目录
        String uploadPath = getServletContext().getRealPath("/upload");// 初始化上传文件后的保存
        try {
            if (ServletFileUpload.isMultipartContent(request)) {
                 DiskFileItemFactory factory = new DiskFileItemFactory();
                 //指定在内存中缓存数据大小,单位为byte,这里设为1Mb  
                factory.setSizeThreshold(1 * 1024 * 1024);   
                //设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录  
                factory.setRepository(new File(tmpDir));   
                ServletFileUpload sfu = new ServletFileUpload(factory);  
                 // 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb  
                sfu.setFileSizeMax(100 * 1024 * 1024);  
                //指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb  
                sfu.setSizeMax(100 * 1024 * 1024);   
                sfu.setHeaderEncoding("UTF-8"); //设置编码,因为我的jsp页面的编码是utf-8的  
                FileItemIterator fii = sfu.getItemIterator(request);// 解析request请求  
                uploadPath = uploadPath + "\\jbpm\\"; // 选定上传的目录此处为当前目录  
                if (!new File(uploadPath).isDirectory()){  
                    new File(uploadPath).mkdirs(); //选定上传的目录此处为当前目录,没有则创建  
                }  
                  
                int index = 0;  
                while (fii.hasNext()) {  
                    FileItemStream fis = fii.next();// 从集合中获得一个文件流  
                    if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域  
                        String fileName = fis.getName();// 获得上传文件的文件名                        
                        BufferedInputStream in = new BufferedInputStream(fis.openStream());   
                        BufferedOutputStream out = new BufferedOutputStream(  
                                new FileOutputStream(new File(uploadPath + "\\" + fileName)));  
                        Streams.copy(in, out, true); // 开始把文件写到你指定的上传文件夹  
                        index++;  
                    }  
                }  
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setContentType("text/html");
        response.getWriter().print("{success:true}");
    }

上传文件为空:因为:
    <filter-mapping>
        <!--拦截所有的URL请求-->
        <filter-name>struts2</filter-name>
        <!--上传文件会不起作用-->
        <!-- url-pattern>/*</url-pattern -->
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

原因就是因为在web.xml中配置了Struts的filter
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

改成

<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
</filter-mapping>

就可以了


posted on 2010-12-04 11:33 单飞 阅读(744) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: