<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

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

    ?1 public ? class ?User? extends ?Principal? {
    ?2 ???? private ? byte []?photo;??? // 考慮與各種數(shù)據(jù)庫兼容問題
    ?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 >


    引用默認(rèn)的攔截器棧,棧里已經(jīng)包含F(xiàn)ileUploadInterceptor攔截,這里配置FileUploadInterceptor攔截的參數(shù),設(shè)定上傳的文件為圖片
    格式
    Action類

    ?1 public ? class ?OrganizationAction? extends ?BaseOrganizationAction {
    ?2 ???? private ?File?photo;?? // 與頁面里的上傳文件字段名對(duì)應(yīng)
    ?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);??? // 將上傳的圖片轉(zhuǎn)換為字節(jié)數(shù)組存儲(chǔ)到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中字節(jié)數(shù)組轉(zhuǎn)換為輸出流
    29 ????????????outPhoto.write(user.getPhoto());
    30 ????????????outPhoto.flush();
    31 ????????????outPhoto.close();
    32 ????????}

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

    35 }

    36


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

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


    而我的配置里面是

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


    不知道這會(huì)不會(huì)有什么影響。或者這樣?

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


    http://m.tkk7.com/ronghao 榮浩原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:)
    posted on 2006-06-15 17:14 ronghao 閱讀(665) 評(píng)論(0)  編輯  收藏 所屬分類: 表現(xiàn)層相關(guān)

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2006年6月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    關(guān)注工作流和企業(yè)業(yè)務(wù)流程改進(jìn)。現(xiàn)就職于ThoughtWorks。新浪微博:http://weibo.com/ronghao100

    常用鏈接

    留言簿(38)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    常去的網(wǎng)站

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲精品无码久久久久APP| 久久综合日韩亚洲精品色| 在线亚洲午夜片AV大片| 亚洲高清免费在线观看| 91亚洲国产成人久久精品网站| 91在线老王精品免费播放| 久久久久亚洲AV无码麻豆| 四虎1515hh永久久免费| 亚洲深深色噜噜狠狠网站| 日本人护士免费xxxx视频| 精品免费AV一区二区三区| 亚洲精品美女久久久久99小说| 久久久久久久国产免费看| 国产AV无码专区亚洲AV男同| 久久国产免费观看精品3| 天天爽亚洲中文字幕| 日韩精品视频免费在线观看| 精品国产_亚洲人成在线| 亚洲美日韩Av中文字幕无码久久久妻妇| 午夜免费国产体验区免费的| 亚洲色精品88色婷婷七月丁香| 四虎影视成人永久免费观看视频 | 亚洲成人影院在线观看| 国产精品午夜免费观看网站 | 亚洲成年人免费网站| 成年女人看片免费视频播放器| 亚洲av无码成人影院一区 | 亚洲男人在线无码视频| 成人免费区一区二区三区 | 亚洲天堂免费在线| 亚洲精品成人久久久| 久久久久久AV无码免费网站| 精品国产日韩久久亚洲| 亚洲av麻豆aⅴ无码电影| 97视频免费观看2区| 亚洲AV无码之国产精品| 亚洲激情在线视频| 国产成人啪精品视频免费网| 最近更新免费中文字幕大全| 亚洲国产成人91精品| 狠狠色婷婷狠狠狠亚洲综合|