码迷,mamicode.com
首页 > Web开发 > 详细

struts 文件上传

时间:2017-09-14 16:54:44      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:rename   对象   tom   自动   form表单   code   class   bsp   throws   

1、jsp中form表单的要求

<FORM id=form1 name=form1 action="${pageContext.request.contextPath }/CustomerAction_add"
  method="post" enctype="multipart/form-data" >
  <input type="file" name="photo" />
</FORM>

method:post enctype:multipart/form-data

2、Action中的要求

//上传的文件会自动封装到File对象
//在后台提供一个与前台input type=file组件 name相同的属性
private File photo;
//在提交键名后加上固定后缀FileName,文件名称会自动封装到属性中
private String photoFileName;
//在提交键名后加上固定后缀ContentType,文件MIME类型会自动封装到属性中 
private String photoContentType;
public String add() throws Exception {
    if(photo!=null){
        System.out.println("文件名称:"+photoFileName);
        System.out.println("文件类型:"+photoContentType);
        //将上传文件保存到指定位置
        photo.renameTo(new File("E:/upload/haha.jpg"));
    }
    
    //---------------------------------------------------------------------
    //1 调用Service,保存Customer对象
    cs.save(customer);
    //2 重定向到客户列表Action
    return "toList";
}

以上两步就可以实现struts下文件上传功能

 

struts 文件上传

标签:rename   对象   tom   自动   form表单   code   class   bsp   throws   

原文地址:http://www.cnblogs.com/ms-grf/p/7520428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!