struts添加图片的Action

    技术2022-05-11  90

    public class ProjectAddAction extends Action { InputStream istream;

     public ActionForward execute(ActionMapping mapping, ActionForm actionform,   HttpServletRequest request, HttpServletResponse reponse)   throws Exception {  String v = request.getParameter("v");//如果v有值且为1,则表示是显示该页面,否则表示是在该页面中点击了保存按钮  ProjectAddForm paf = (ProjectAddForm) actionform;//强制转换actionform  ManagerDao md = new ManagerDao();//经理Dao类  ProjectDao pd = new ProjectDao();//项目Dao类  System.out.println("v="+v);  if (v!=null&&v.equals("1")) {//如果是显示页面则跳转到projectadd.jsp页面   List l = md.queryName();//查询经理名称和id,该方法将返回一个list列表   paf.setManagerlist(l);//将该列表存入Actionform中   return mapping.findForward("add");//跳转到projectadd.jsp  } else {   String dir = servlet.getServletContext().getRealPath("/photo");//图片将被提交到该目录中   //------------   Project p = new Project();//新建实体类   p.setPcycle(paf.getPcycle());//从formbean中取值存入实体类中   p.setPintro(paf.getPintro());   p.setPmanager(paf.getPmanager());   p.setPmoney(paf.getPmoney());   p.setPname(paf.getPname());   p.setPstudent(paf.getPstudent());   //   ---从目录中取得照片   FormFile file = paf.getFile();   String filename = file.getFileName();   istream = file.getInputStream();   String path = dir + "//" + filename;   OutputStream ostream = new FileOutputStream(path);   int bytesRead = 0;   byte[] buffer = new byte[8192];   while ((bytesRead = istream.read(buffer, 0, 8192)) != -1) {    ostream.write(buffer, 0, bytesRead);   }   byte[] ph = pd.insertPhoto(path);//   p.setPpicture(ph);//将图片的字节流插入实体类中   file.destroy();   istream.close();   ostream.close();   pd.insert(p);//将实体类通过projectDao类插入数据库   return mapping.findForward("list");//跳转回projectlist.do既projectlist.jsp  }

     }} 


    最新回复(0)