在用户注册界面,要上传一个用户的头像,那么怎么才能做到这个上传后的路径和这个用户绑定呢?

解决方案 »

  1.   

    public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
    // ActionMessages mess=new ActionMessages();

    String dir=servlet.getServletContext().getRealPath("/upload");
    File scfile=new File(dir);
      if(!scfile.exists()){
    scfile.mkdir();
    }
    HtmlFileForm hff=(HtmlFileForm)form;
    FormFile file=hff.getFile();
    String flname=file.getFileName();
    if(flname==null||flname.equals("")){
    this.setFileErrorMessage("sssss");
    request.setAttribute("error1", this.fileErrorMessage);
    return mapping.findForward("failure");
    }

    // ナミカマホトシ�タ獎ヘ
    int imgType=flname.lastIndexOf(".");
    String imgTypeStr=flname.substring(imgType);

    //スォホトシ�テ逸ヲタ昕ノオアフ�ネユニレ+ヒ貍エハ�
    String date=DNSUtility.getCurrentYeanMonthDay();
    String[] dateStr=date.split("/");
    String dateName=dateStr[0]+dateStr[1]+dateStr[2];

    Random random = new Random();
    int rdInt=random.nextInt(1000000000);
    String rdStr=String.valueOf(rdInt);

    String fname=dateName+rdStr+imgTypeStr;
    //ア」エ豬スハ�センソ� String urlTemp="./upload/";
    String picUrl=urlTemp+fname;
    //ナミカマホトシ�エ。
    if(file.getFileSize()>2097152){
    this.setSizeErrorMessage("カヤイサニャヌ�イ鮓ヤト揵マエォオトヘシニャハヌキ�ネキ(ホトシ�メムセュウャケ�2M)!");
    request.setAttribute("error2", this.sizeErrorMessage);
    return mapping.findForward("failure");
    }
    // ナミカマホトシ�タ獎ヘ
    // ア」エ�.swfホトシ�オスflashア� if(imgTypeStr.equals(".swf")){
    String size=Integer.toString(file.getFileSize())+"bytes";
    InputStream streamIn=file.getInputStream();
    OutputStream streamOut=new FileOutputStream(dir+"/"+fname);
    int bytesRead=0;
    byte[] buffer=new byte[8192];
    while((bytesRead=streamIn.read(buffer,0,8192))!=-1){
    streamOut.write(buffer, 0, bytesRead);
    }
    streamOut.close();
    streamIn.close();
    hff.setFname(fname);
    hff.setSize(size);
    file.destroy();
    this.pictureComponentsService.insertFlashUrl(picUrl);
    return mapping.findForward("success");
    }
    if(!imgTypeStr.equals(".jpg")&&!imgTypeStr.equals(".gif")&&!imgTypeStr.equals(".jpeg")&&!imgTypeStr.equals(".png")&&!imgTypeStr.equals(".swf")){
    this.setTypeErrorMessage("カヤイサニャヌ�イ鮓ヤト揵マエォオトヘシニャタ獎ヘ!");
    request.setAttribute("error3",this.typeErrorMessage);
    return mapping.findForward("failure");
    }

    String size=Integer.toString(file.getFileSize())+"bytes";
    InputStream streamIn=file.getInputStream();
    OutputStream streamOut=new FileOutputStream(dir+"/"+fname);
    int bytesRead=0;
    byte[] buffer=new byte[8192];
    while((bytesRead=streamIn.read(buffer,0,8192))!=-1){
    streamOut.write(buffer, 0, bytesRead);
    }
    streamOut.close();
    streamIn.close();
    hff.setFname(fname);
    hff.setSize(size);
    file.destroy();
    this.pictureComponentsService.insertPicUrl(picUrl);
    return mapping.findForward("success");

    }
      

  2.   

    谢谢啦,LS的
    能有servlet+jsp实现的吗?
    一般论坛里面的注册信息那里的用户名和头像的路径是怎么绑定的?
      

  3.   

    声明一个File类实例 用来代表上传的头像 然后把File的路径返回或者存到数据库里就行了