struts添加图片的Action

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值