主题: 写了一个上传图片(Struts)版始终不成功
作者: pollsbaby, 发布日期: 2009-02-17 15:38:28, 浏览数: 11433
        String id = (String) request.getParameter("id");
        String imgTitle = (String) request.getParameter("imgTitle");
        String imgWidth = (String) request.getParameter("imgWidth");
        String imgHeight = (String) request.getParameter("imgHeight");
        String imgBorder = (String) request.getParameter("imgBorder");
        String url = "";
        String out = "";
        
        Hashtable files = this.getMultipartRequestHandler().getFileElements();
        for (Map.Entry<String, FormFile> entry : (Set<Map.Entry<String, FormFile>>) (files.entrySet())) {
            FormFile ff = entry.getValue();
            String name = ff.getFileName();
            if (StringUtils.isEmpty(name)) {
                out=this.alert("请选择文件。");
                
            }
            if(!StringUtils.isEmpty(name)&&!ImageUtils.isPicFile(name))
            {
                out=this.alert("上传文件扩展名是不允许的扩展名。");
            }
            if (!StringUtils.isEmpty(name)&&ImageUtils.isPicFile(name)) {
                String img_format = "." + StringUtils.split(name, '.')[1];
                Pic pic = new Pic();
                String picName = "";
                try {
                    picName = this.photoService.uppic(this.getUserSession().getUid(), img_format, ff.getInputStream());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    out=this.alert("请选择文件。");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    out=this.alert("上传文件失败。");
                }
                pic.setFilename(picName);
                pic.setFilepath("");
                pic.setDateline(new Date());
                pic.setUid(this.getUserSession().getUid());
              
                url = Constants.PHOTOS_PATH + pic.getUid() + "/" + ImageUtils.getUserMidPic(pic);
                
                String temp = "parent.KE.plugin[\"image\"].insert(\'"+id+"','"+url+"','"+imgTitle+"','"+imgWidth+"','"+imgHeight+"','"+imgBorder+"');";
                StringBuffer sb = new StringBuffer();
                sb.append("<html>");
                sb.append("<head>");
                sb.append("<title>Insert Image</title>");
                sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
                sb.append("</head>");
                sb.append("<body>");
                sb.append("<script type=\"text/javascript\">"+temp+";</script>");
                sb.append("</body>");
                sb.append("</html>");
                out = sb.toString();
            }
        }
        
        try {
            //输出ajax内容
            response.setHeader("Content-Type", "text/html;charset=UTF-8");
           response.getWriter().write(out );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
作者: pollsbaby, 发布日期: 2009-02-17 16:10:52
搞定!
回复
作者: xiaocase, 发布日期: 2009-03-13 18:30:44
楼上的兄弟怎么搞定的,能否说一下呢。
回复
发表新帖 发表回复