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

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

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

    Greedy的工作室

    posts - 23,comments - 7,trackbacks - 0
    本文用的是Struts1.1的org.apache.struts.upload.FormFile類。很方便,不用自己寫。也不用寫一個jsp調用jspsmartupload就可以搞定。

    FormFile的幾個方法:
    String getContentType();
    byte[] getFileData();
    String getFileName();
    int getFileSize();
    如果有必要可以在validate里調用一下做一些判斷。

    選擇上傳文件頁面:selfile.jsp

    --------------------------------------------------------------------------------
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
    <html:html>
    <html:form action="/uploadsAction.do" enctype="multipart/form-data">
    <html:file property="theFile"/>
    <html:submit/>
    </html:form>
    </html:html>

    --------------------------------------------------------------------------------
    UpLoadAction.java
    --------------------------------------------------------------------------------

    import java.io.*;
    import javax.servlet.http.*;
    import org.apache.struts.action.*;
    import org.apache.struts.upload.FormFile;

    public class UpLoadAction extends Action {
     public ActionForward execute(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response)
         throws Exception {
       if (form instanceof uploadsForm) {//如果form是uploadsForm

           UpLoadForm theForm = (UpLoadForm ) form;
           FormFile file = theForm.getTheFile();//取得上傳的文件
           try {
             InputStream stream = file.getInputStream();//把文件讀入
             String filePath = request.getRealPath("/");//取當前系統路徑
             OutputStream bos = new FileOutputStream(filePath + "/" +
                                                     file.getFileName());//建立一個上傳文件的輸出流
             //System.out.println(filePath+"/"+file.getFileName());
             int bytesRead = 0;
             byte[] buffer = new byte[8192];
             while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
               bos.write(buffer, 0, bytesRead);//將文件寫入服務器
             }
             bos.close();
             stream.close();
           }catch(Exception e){
             System.err.print(e);
           }
           //request.setAttribute("dat",file.getFileName());
           return mapping.findForward("display");
       }
       return null;
     }
    }


    --------------------------------------------------------------------------------
    UpLoadForm.java
    --------------------------------------------------------------------------------

    import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.*;
    import org.apache.struts.upload.*;

    public class UpLoadForm extends ActionForm {
     public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
     protected FormFile theFile;
     public FormFile getTheFile() {
         return theFile;
     }
     public void setTheFile(FormFile theFile) {
         this.theFile = theFile;
     }
     public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
     {
         ActionErrors errors = null;
         //has the maximum length been exceeded?
         Boolean maxLengthExceeded = (Boolean)
                 request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
         if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
         {
             errors = new ActionErrors();
             errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
         }
         return errors;

     }
    }
    //這是相對應的form,還有其他屬性可以設置,具體可以參考struts的上傳例子。


    --------------------------------------------------------------------------------
    struts-config.xml
    --------------------------------------------------------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
    <struts-config>
     <form-beans>
       <form-bean name="uploadsForm" type="UpLoadForm" />
     </form-beans>
     <action-mappings>
       <action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
         <forward name="display" path="/display.jsp" />
       </action>
     </action-mappings>
    </struts-config>
    <!--display.jsp就是隨便寫一個成功頁-->
    posted on 2008-06-27 10:47 greedy 閱讀(187) 評論(0)  編輯  收藏 所屬分類: Struts

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


    網站導航:
     
    主站蜘蛛池模板: 好男人视频在线观看免费看片| 91香蕉成人免费网站| 亚洲v国产v天堂a无码久久| 中国亚洲呦女专区| 免费无码AV片在线观看软件| 亚洲一区二区三区四区视频| 91香蕉成人免费网站| 精品亚洲AV无码一区二区| 黄瓜视频影院在线观看免费| 亚洲高清有码中文字| 国产小视频免费观看| 免费无码一区二区| 狠狠色伊人亚洲综合成人| 久久久久久AV无码免费网站| 亚洲精品第五页中文字幕| 国产麻豆视频免费观看| 亚洲变态另类一区二区三区 | 亚洲一区二区中文| 中文字幕av无码无卡免费| 亚洲av永久无码精品秋霞电影秋 | 亚洲情侣偷拍精品| 今天免费中文字幕视频| 亚洲美女一区二区三区| 国产免费AV片在线播放唯爱网| 亚洲精品伦理熟女国产一区二区| 国产免费资源高清小视频在线观看| xxxx日本在线播放免费不卡| 久久亚洲精品成人| 嫖丰满老熟妇AAAA片免费看| 亚洲第一se情网站| 国产精品亚洲A∨天堂不卡| 欧美最猛性xxxxx免费| 日韩精品免费一线在线观看| 五月天网站亚洲小说| 免费毛片在线播放| 最近的2019免费中文字幕| 亚洲日本人成中文字幕| 国产精品亚洲不卡一区二区三区| 久久久久久精品免费看SSS | 国产在线观看免费完整版中文版 | 亚洲国产精品一区|