在struts2中实现图片上传:
前端:
        <form name="infoForm" method="post"  action="AdminAdBanner!update" enctype="multipart/form-data">      <tr>
           <td align="right">上传图片</td>
            <td align="left"><input name="uploadImg" type="file" editable="false"/></td>
      </tr>
        <tr>
                 <td colspan="2"><img src="images/menber_04.jpg" width="142" height="32" onclick="adBannerSubmit()" /></td>
                </tr>
     </form>
Action:
         private String allowType = "jpg,gif"; private String title;

private File uploadImg;

private String uploadContentType;

private String uploadFileName;         public String update() throws Exception {
        //未登陆该请求跳转登陆页面
        if(getSession()== null || getSession().getAttribute("adminId") == null){
            return "doLoginAdmin";
        }
        //图片上传
       
        try {
         String[] types = this.getAllowType().split(",");
         if (!filterType(types)) {
         ActionContext.getContext().put("typeError", "格式不正确!");
         return "info";
         }
         // 以服务器的文件保存地址和原文件名建立上传文件输出流
         FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/images/upload_img/")+ getUploadFileName());
         FileInputStream fis = new FileInputStream(getUploadImg());
         byte[] buffer = new byte[2048];
         int len = 0;
         while ((len = fis.read(buffer)) > 0) {
         fos.write(buffer, 0, len);
         }
         fis.close();
         fos.close();
         } catch (Exception e) {
         // 日志
         throw new Exception("上传的文件格式不符或文件太大!");
        }帮忙看下有什么问题,因为我也是刚接触,是不是一些细节有问题?为什么运行后不报错,但是上传后没反应呢?

解决方案 »

  1.   

    好郁闷啊~~就是不知道哪里有问题
    <form name="infoForm" method="post" action="AdminAdBanner!update" enctype="multipart/form-data">
    public String update() throws Exception {
      //未登陆该请求跳转登陆页面
      if(getSession()== null || getSession().getAttribute("adminId") == null){
      return "doLoginAdmin";
      }
      //图片上传
        
      try {
      String[] types = this.getAllowType().split(",");
      if (!filterType(types)) {
      ActionContext.getContext().put("typeError", "格式不正确!");
      return "info";
      }
      // 以服务器的文件保存地址和原文件名建立上传文件输出流
      FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/images/upload_img/")+ getUploadFileName());
      FileInputStream fis = new FileInputStream(getUploadImg());
      byte[] buffer = new byte[2048];
      int len = 0;
      while ((len = fis.read(buffer)) > 0) {
      fos.write(buffer, 0, len);
      }
      fis.close();
      fos.close();
      } catch (Exception e) {
      // 日志
      throw new Exception("上传的文件格式不符或文件太大!");
      }
      

  2.   

    数据没有提交吧,楼主用的是个图片按钮来提交的,看看那个adBannerSubmit方法调用没
      

  3.   

    什么问题呢?
    控制台有什么内容?
    首先确定你的包是不是对了,全了?
    然后确定表单是否提交!可以打个断点看看进没进action;
    然后进去后看看文件传进来没有,struts2文件上传;文件类型和大小可以在struts的配置文件中配置的,不必再程序中再去写;可以在网上查下,很多的,可以借鉴别人好的做法!
      

  4.   

    楼上说了很详细的debug方法~~搞定了 问题在于
    private File uploadImg;private String uploadContentType;private String uploadFileName;
     名字没关联到一起吧,upload封装的方法要求三个名字前面要一致的如uploadContentType要换成 uploadImgContentType,这样关联起来才可能把文件类型和名字读进去。改进了代码,用当前时间作为文件名保存,分享一下。  
     private File uploadImg;
     private String uploadImgFileName;
     private String uploadImgContentType;
     public String update() throws Exception {
    String realpath= ServletActionContext.getServletContext().getRealPath("/images/upload_images");
            if(uploadImg!=null){
             String[] str = uploadImgFileName.split("\\.");
            
         File savedir = new File(realpath);
         if(!savedir.exists()){
         savedir.mkdirs();
         }
         Date now = new Date();
         SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
         String fileName = format.format(now)+"."+str[str.length-1];
         File savefile = new File(savedir,fileName);
             FileUtils.copyFile(uploadImg,savefile);
             ActionContext.getContext().put("message", "上传成功");}