day3——关于<s:if/>和文件上传

本文介绍了一个使用Struts2框架进行文件上传的具体实现过程,包括JSP页面的表单设置、Action类中的属性及方法定义,并展示了如何将文件保存到服务器指定位置。

一个小的注意点

<s:if test='#backyear==#yearatd'>selected="selected"</s:if> 

<s:if test='%{#backmonth==-1}'>selected="selected"</s:if>

 

文件上传(struts2)

jsp:

  <form name="myform" method="post" action="employeesAdd" enctype="multipart/form-data">

  <p><span>上传头像:</span><input type="file" class="txtinput" name="upload" /></p>

Action:

  //上传文件 
  private File upload;

  //上传文件名
  private String uploadFileName;
  //上传文件类型
  private String uploadContentType;

 

  //....getter、setter

  

  public String AddEmployee(){

    //项目路径
    String root = ServletActionContext.getServletContext().getRealPath("/");

    //文件存放目录
    String filepath = root+File.separatorChar+"upload";
  try {

    //复制文件到服务器中
    FileUtils.copyFile(upload, new File(filepath,uploadFileName));

    //访问文件的路径(File.separatorChar相当于"/")
    String imgurl = "upload"+File.separatorChar+uploadFileName;


    employee.setEpicture(imgurl);
    
  } catch (IOException e) {
    // TODO Auto-generated catch block
  e.printStackTrace();
  }
  employeesService.addEmployee(employee);
  return SUCCESS;
  }

转载于:https://www.cnblogs.com/whisper527/p/6498863.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值