<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 193,  comments - 520,  trackbacks - 0

    開發中遇到這么一個要求,用戶USER這個PO里需要保存用戶的照片。一般情況下有兩種處理方法:一是直接保存圖片到數據庫;二是保存圖片到服務器端,PO字段保存一個聯接。考慮到用戶照片一般較小,采用第一種方法。

    ?1 public ? class ?User? extends ?Principal? {
    ?2 ???? private ? byte []?photo;??? // 考慮與各種數據庫兼容問題
    ?3 ???? public ? byte []?getPhoto()? {
    ?4 ???????? return ?photo;
    ?5 ????}

    ?6
    ?7 ???? public ? void ?setPhoto( byte []?photo)? {
    ?8 ???????? this .photo? = ?photo;
    ?9 ????}

    10 ????..
    11 }

    12


    頁面里面,直接用webwork的FileUploadInterceptor攔截

    < @ww.form??enctype = " multipart/form-data " ?action = " doUpdateUser " ??method = " post " >
    < image?name = " img1 " ?src = " /getUserPhoto.action?userId=${user.id} " ?width = 90 ?height = 120 ?border = " 0 " ></ image >
    < @ww.file?label = " %{getText('user.photo')} " ?name = " photo " ? />
    </ @ww.form >


    xwork.xml里的配置

    ??????? < action?name = " doUpdateUser " ? class = " com.ronghao.organization.action.OrganizationAction " ?method = " updateUser " >
    ????????????
    < interceptor - ref?name = " defaultStack " >
    ????????????????
    < param?name = " fileUpload.allowedTypes " >
    ?????????????????????image
    / x - png,image / gif,image / pjpeg
    ????????????????
    </ param >
    ????????????
    </ interceptor - ref >
    ????????????
    < result?name = " success " >/ organization / user.ftl </ result >
    ????????
    </ action >

    ????????
    < action?name = " getUserPhoto " ? class = " com.ronghao.organization.action.OrganizationAction " ?method = " getUserPhoto " >
    ????????????
    < result >/ organization / user.ftl </ result >
    ????????
    </ action >


    引用默認的攔截器棧,棧里已經包含FileUploadInterceptor攔截,這里配置FileUploadInterceptor攔截的參數,設定上傳的文件為圖片
    格式
    Action類

    ?1 public ? class ?OrganizationAction? extends ?BaseOrganizationAction {
    ?2 ???? private ?File?photo;?? // 與頁面里的上傳文件字段名對應
    ?3 ???? private ?OutputStream?outPhoto
    ?4
    ?5 ????? /**
    ?6 ?????*?更新用戶信息
    ?7 ?????*
    ?8 ?????*? @return
    ?9 ?????*? @throws ?Exception
    10 ????? */

    11 ???? public ?String?updateUser()? throws ?Exception? {
    12 ???????? // 以下為保存圖片,視情況修改
    13 ???????? if ?( null ? != ?photo)? {
    14 ????????????FileInputStream?file? = ? new ?FileInputStream(photo);
    15 ???????????? int ?length? = ?file.available();
    16 ???????????? byte []?buffer? = ? new ? byte [length];
    17 ????????????file.read(buffer);
    18 ????????????file.close();
    19 ????????????user.setPhoto(buffer);??? // 將上傳的圖片轉換為字節數組存儲到PO中
    20 ????????}

    21 ????????organizationService.updateUser(user);
    22 ???????? return ?SUCCESS;
    23 ????}

    24
    25 ???? public ?String?getUserPhoto()? throws ?Exception? {
    26 ????????user? = ?organizationService.getUserById(userId);
    27 ???????? if ?(user.getPhoto()? != ? null )? {
    28 ????????????outPhoto? = ? this .getResponse().getOutputStream();? // 將PO中字節數組轉換為輸出流
    29 ????????????outPhoto.write(user.getPhoto());
    30 ????????????outPhoto.flush();
    31 ????????????outPhoto.close();
    32 ????????}

    33 ???????? return ?SUCCESS;
    34 ????}

    35 }

    36


    這樣就OK了,考慮一個問題,就是action 必須要指定一個result ,實際這里的圖片顯示僅僅是要一個輸出流

    < image?name = " img1 " ?src = " /getUserPhoto.action?userId=${user.id} " ?width = 90 ?height = 120 ?border = " 0 " ></ image >


    而我的配置里面是

    < result >/ organization / user.ftl </ result >


    不知道這會不會有什么影響。或者這樣?

    < result >/ organization / blank.ftl </ result >


    http://m.tkk7.com/ronghao 榮浩原創,轉載請注明出處:)
    posted on 2006-06-15 17:14 ronghao 閱讀(670) 評論(0)  編輯  收藏 所屬分類: 表現層相關

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2006年6月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    關注工作流和企業業務流程改進。現就職于ThoughtWorks。新浪微博:http://weibo.com/ronghao100

    常用鏈接

    留言簿(38)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    常去的網站

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲色欲色欲www在线播放| 久久精品九九亚洲精品| 亚洲AV无码一区二区三区久久精品 | 无码国产精品一区二区免费3p| 国产偷国产偷亚洲高清日韩 | 亚洲一区在线观看视频| 免费无码中文字幕A级毛片| 国产在线一区二区综合免费视频| 亚洲熟妇少妇任你躁在线观看| 999zyz**站免费毛片| 亚洲精品麻豆av| 成在线人视频免费视频| 中文字幕在线亚洲精品| 中文在线免费看视频| 亚洲精品无码成人片久久| 免费观看久久精彩视频| 老司机亚洲精品影院无码| 亚洲第一网站免费视频| 亚洲人成激情在线播放| 日本无卡码免费一区二区三区| 亚洲GV天堂GV无码男同| 亚洲第一区在线观看| 在线观看免费视频一区| 在线免费观看亚洲| 在线观看免费人成视频| 边摸边吃奶边做爽免费视频99 | 永久免费无码网站在线观看个| 国产亚洲精品看片在线观看| 午夜爽爽爽男女免费观看影院| 亚洲最大视频网站| 国产资源免费观看| 韩日电影在线播放免费版| 亚洲午夜电影在线观看高清| 国产免费观看网站| 久久免费精品一区二区| 亚洲人成网站日本片| 亚洲国产精品国产自在在线| 99re视频精品全部免费| 日本亚洲欧美色视频在线播放 | 亚洲heyzo专区无码综合| 久久久久亚洲精品天堂久久久久久 |