您的位置:首页 > 其它

UserAction extends ActionSupport implements ModelDriven<User> .tmp 上传图片文件

2016-04-19 22:12 423 查看
实现了ModelDriven<User>接口和继承自和ActionSupport的情况下我想要传文件

以下是我原先的写法

jsp页面:

<s:form action="User_regUser" method="POST" enctype="multipart/form-data">

        <ul>

           <li>

              <label for="img">头像</label>

              <s:file id="img"  name="img"/>

UserAction页面:

@Controller(value="UserAction")  @Scope("prototype")

public class UserAction extends ActionSupport implements  ModelDriven<User>{

        User user=new User();

        private File img;

    private String  imgContentType;

    private String  imgFileName;

    public File getImg() {

        return img;

    }    

public void setImg(File img){

        this.img = img;

    }

....

imgContentType

imgFileName的get set 也有

 @Resource(name="UserService")    UserService u_service;

   public User getModel(){      

     if(user==null){          

     return new User();      

    }       

    return user;   

}  

......//处理头像上传      

   String path = ServletActionContext.getServletContext().getRealPath("/upload");    

  //这里有一小段测试       

     img=getImg();       

     System.out.println(getModel().getImg());

    结果是 D:\tomcat\apache-tomcat-7.0.59\work\Catalina\localhost\computer\upload_2095952d_1542eae06c2__8000_00000004.tmp       

     System.out.println(img);

   我在这里获取到的是null   

           

if (img!=null) {          

 try {             

  InputStream is =new FileInputStream(img);            

   String fileContentType=this.getImgContentType();        

       //判断指定文件的扩展名,生成相应的随机码

                if (fileContentType.equals("image/jpeg")||fileContentType.equals("image/pjpeg")) {

                    //生成随机码

                   imgFileName=UUID.randomUUID().toString()+".jpg";

               } else if(fileContentType.equals("image/gif")){

                    imgFileName=UUID.randomUUID().toString()+".gif";

                }else if (fileContentType.equals("image/png")) {

                   imgFileName=UUID.randomUUID().toString()+".png"; 

             }             

     File file=new File(path,imgFileName);            

     OutputStream os =new FileOutputStream(file);             

     byte[] b =new byte[1024];             

     int bs =0;             

     while((bs=is.read(b))>0){                 

          os.write(b,0,bs);              

     }              

      is.close();              

     os.close();                       

    } catch (Exception e) {            

       e.printStackTrace();          

    }               

}  

  

  if (img!=null) {    

    getModel().setImg("upload/"+imgFileName);

   } else {

           getModel().setImg("upload/NoImage.jpg");

  }     

  File img为null当然它每次给我存到数据库里 的就是upload/NoImage.jpg     

       试图把.tmp文件处理成我想要的图片存起来 没找到办法



修改了写法

jsp页面:



<s:form action="User_regUser" method="POST" enctype="multipart/form-data">

  <ul>

  <li>

  <label for="image">头像</label>

  <s:file id="image" name="image"/>

UserAction页面:

@Controller(value="UserAction")  @Scope("prototype")

public class UserAction extends ActionSupport implements ModelDriven<User>{

       User user=new User();

        private File image;

    private String imageContentType;

    private String imageFileName;

        @Resource(name="UserService")

    UserService u_service;

    public User getModel(){

       if(user==null){

           return new User();

        }      

 return user;  

 }   

  ....    

   //处理头像上传   

    String path = ServletActionContext.getServletContext().getRealPath("/upload"); 

             image=getImage();

     System.out.println("uu"+getModel().getImg());

       System.out.println("vv"+image);

输出为

uunull

vvD:\tomcat\apache-tomcat-7.0.59\work\Catalina\localhost\computer\upload_2095952d_1542eae06c2__8000_00000010.tmp

随机码不同是因为 我换了张图片插入

if (image!=null) {

           try {  

             InputStream is =new FileInputStream(image);  

             String fileContentType=this.getImageContentType();  

             //判断指定文件的扩展名,生成相应的随机码

                if (fileContentType.equals("image/jpeg")||fileContentType.equals("image/pjpeg")) {

                    //生成随机码 

                 imageFileName=UUID.randomUUID().toString()+".jpg";

               } else if(fileContentType.equals("image/gif")){

                   imageFileName=UUID.randomUUID().toString()+".gif"; 

             }else if (fileContentType.equals("image/png")) { 

                 imageFileName=UUID.randomUUID().toString()+".png";

               }              

 File file=new File(path,imageFileName); 

             OutputStream os =new FileOutputStream(file);

               byte[] b =new byte[1024];  

             int bs =0;        

       while((bs=is.read(b))>0){

                   os.write(b,0,bs);

               }             

  is.close();

                os.close();   

                        } catch (Exception e) { 

             e.printStackTrace();

           }             

  }      

 if (image!=null) {

           getModel().setImg("upload/"+imageFileName);       

     } else {

            getModel().setImg("upload/NoImage.jpg");

       }

 ....数据库里存入upload/af08ca49-fb95-40f5-bbde-6e6f10a2a8f6.jpg

 查看目录D:\tomcat\apache-tomcat-7.0.59\webapps\computer\upload下

我看到了我上传的图片    问题解决。



总结   

         ModelDriven<User>(我的数据库字段名也为img)获取了我name=img的文件,并且显示为一个.tmp文件在tomcat catalina下,于是File img原本通过struts的

ActionSupport获取文件的结果为null。

    我把jsp页面上的struts文件标签的name修改为image ,ModelDriven<User>(我的数据库字段名也为img)当然找不到和img对应的表单项,于是为null。这时

我的File image通过struts的ActionSupport获取到了到.tmp文件,同样存放在tomcat catalina 下。但是File 获取到了以后,后面的代码能顺利执行。

    既然都是.tmp文件,或许我可以不用更改jsp上的name属性值,尝试吧user.getImg()得到的对象赋值给File img 第一个程序也可以进行下去。只是

不知道imgContentType、imgFileName在img是通过user.getImg()赋值的情况下能不能得到相应的值。如果可以,那么就没有什么问题了。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: