<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類。很方便,不用自己寫。也不用寫一個(gè)jsp調(diào)用jspsmartupload就可以搞定。

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

    選擇上傳文件頁(yè)面: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("/");//取當(dāng)前系統(tǒng)路徑
             OutputStream bos = new FileOutputStream(filePath + "/" +
                                                     file.getFileName());//建立一個(gè)上傳文件的輸出流
             //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);//將文件寫入服務(wù)器
             }
             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;

     }
    }
    //這是相對(duì)應(yīng)的form,還有其他屬性可以設(shè)置,具體可以參考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就是隨便寫一個(gè)成功頁(yè)-->
    posted on 2008-06-27 10:47 greedy 閱讀(187) 評(píng)論(0)  編輯  收藏 所屬分類: Struts

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久精品九九亚洲精品| 国产亚洲一区二区三区在线| 亚洲日本乱码一区二区在线二产线| 成全动漫视频在线观看免费高清版下载 | 亚洲成a人片在线观看播放| 99久久精品毛片免费播放| 亚洲综合无码精品一区二区三区 | 亚洲嫩模在线观看| 2019中文字幕在线电影免费| 中文字幕在线观看亚洲| 日韩亚洲国产高清免费视频| 亚洲第一成人在线| 国产免费卡一卡三卡乱码| 深夜福利在线免费观看| 亚洲国产免费综合| 国产午夜无码片免费| 精品亚洲成a人片在线观看| 最近2019免费中文字幕视频三| 亚洲一区二区三区在线观看蜜桃| 毛片A级毛片免费播放| 看一级毛片免费观看视频| 国产亚洲美女精品久久久2020| 中文字幕日本人妻久久久免费| 亚洲精品韩国美女在线| 日韩精品视频免费在线观看| 有色视频在线观看免费高清在线直播| 久久国产亚洲精品麻豆| 可以免费看黄视频的网站| 人人狠狠综合久久亚洲| 亚洲色欲一区二区三区在线观看| 麻豆高清免费国产一区| 韩国亚洲伊人久久综合影院| 久久影视国产亚洲| 成人在线免费看片| 一级毛片正片免费视频手机看| 老司机亚洲精品影院| 国产伦精品一区二区三区免费下载| 两个人www免费高清视频| 亚洲av无码片在线观看| 在线亚洲人成电影网站色www| 亚洲人成免费电影|