struts2上传图片和显示图片 struts2上传图片和显示图片

struts2上传图片和显示图片

一、图片上传

1.在jsp页面中

<s:form action="company" method="post"enctype="multipart/form-data">

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

<s:submit value="添加"/>

</s:form>

2.Action 中

public class CompanyAction extends ActionSupport {
    CompanyService companyService;
    private Pager pager=new Pager();
    private Company company;
    private File file;
    private String fileFileName;

……

get、set方法

……

public String execute() throws Exception{

return add();

}

public String add() throws Exception{

  if(file!=null){
   addFile();
  }
  fpService.add(findPerson);
  return "add";

}

public String addFile() throws Exception {
  String o="";
  if (file != null) {
   
    if (this.getFileFileName() != null) {
     try {
      
      
      String mailTime = DateUtil
        .mailDate(new java.util.Date());
      
      company.setFp_logpath("upload/"
        + mailTime
        + "_"
        + "."
        + StringUtil.getExtension(this
          .getFileFileName()));
           InputStream is = new FileInputStream(file);
      String root = ServletActionContext.getRequest()
        .getRealPath("/upload");
      File diskFile = new File(root, mailTime
        + "_"
        
        + "."
        + StringUtil.getExtension(this
          .getFileFileName()));
      OutputStream os = new FileOutputStream(diskFile);
      byte[] buffer = new byte[8192];
      int length = 0;
      while ((length = is.read(buffer)) > 0) {
       os.write(buffer, 0, length);
      }
      is.close();
      os.close();
      
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   
  }
  return o;
 }

}

3.CompanyServiceImpl

public class CompanyServiceImpl implements CompanyService {
  
     public void add(Company company){
      commonDao.add(company);
     }

     public Company get(int id){
      return (Company )commonDao.get(Company.class, id);
     }
     public void update(Company fp){
      commonDao.update(fp);
     }

}
二、显示图片(view)

1.jsp页面

<s:form action="companyAction" method="post">

  <img src="/<s:property value="company.logpath" /> " alt="企业logo" width="150px" height="50px"/>

</s:form>

2.Action中

与一般的view 方法相同

三、修改company换另一幅图片

1.Action 中首先调用get方法获得要修改的实体(与一般情况相同)

2.在jsp页面中

<s:form action="companyAction" method="post"  enctype="multipart/form-data">

    <s:if test="%{!company.logpath.equals('')}">
           <s:hidden name="company.logpath"/>
          </s:if>
          <s:file name="file" id="file" ></s:file>

<s:submit value="修改"/>

</s:form>

3.在Action中,调用update方法

public String update()throws Exception{
  HttpServletRequest request = ServletActionContext.getRequest();
   Company company1=companyService.get(company.getId());
  company.setLogpath("");
  if(file!=null&&fileFileName!=null&&!fileFileName.equals("")){
   addFile();
  }else{
   if(null!=company1.getLogpath()&&!company1.getLogpath().equals("")){
    company.setLogpath(company1.getLogpath());
   }
  }
  companyService.update(company);
  return "update";
 }

 四、以上操作的共同条件

在webroot下建立upload包用来放置图片

猜你喜欢

转载自blog.csdn.net/qq_32444825/article/details/80504764