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

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

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

    和爪哇一起走過的日子

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      12 隨筆 :: 0 文章 :: 6 評論 :: 0 Trackbacks

    使用spring MVC框架進行文件上傳,步驟如下:

    1:配置web.xml文件。定義DispatcherServlet,DispatcherServlet處理的請求(.htm)也在同一個web.xml文件里使用url-mapping定義映射。

     <servlet>
      <servlet-name>upload</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
     </servlet>
     
     <servlet-mapping>
      <servlet-name>upload</servlet-name>
      <url-pattern>*.htm</url-pattern>
     </servlet-mapping>
    2:定義upload-servlet.xml文件。
    <bean id="multipartResolver"
           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- set the max upload size100MB -->
            <property name="maxUploadSize">
            <value>104857600</value>
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
       </bean>
     <bean id="urlMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">       
     <property name="mappings">           
      <props>               
      <prop key="/upload.htm">uploadController</prop>           
      </props>       
     </property>   
     </bean>
         <bean id="uploadController" class="FileUploadController">
          <property name="commandClass"><value>FileUploadBean</value></property>
          <property name="uploadDir"><value>E:/</value></property>
          <property name="formView"><value>fail</value></property>
      <property name="successView"><value>confirmation</value></property>
    </bean>  
    3:定義控制類,commandClass及方法。控制類中最重要的方法是initBinder()它給spring注冊了一個編輯器對
    request中的multipart實體進行處理,如果沒有這個方法,上傳將不能進行。
    <--------------------------控制類------------------->
    public class FileUploadController extends SimpleFormController {
        private static Log log =
            LogFactory.getLog(FileUploadController.class);
        private String uploadDir;//上傳文件路徑

        protected ModelAndView onSubmit(HttpServletRequest request,
                HttpServletResponse response, Object cmd, BindException errors)
                throws Exception {

                FileUploadBean bean = (FileUploadBean) cmd;
                byte[] bytes = bean.getFile();
              
                //cast to multipart file so we can get additional information
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");

                String uploadDir = this.getUploadDir();

                File dirPath = new File(uploadDir);
                if (!dirPath.exists()) {
                    dirPath.mkdirs();
                }
                String sep = System.getProperty("file.separator");
                if (log.isDebugEnabled()) {
                    log.debug("uploading to: " + uploadDir + sep +
                    file.getOriginalFilename());
                    }
                File uploadedFile = new File(uploadDir + sep
                        + file.getOriginalFilename());
                FileCopyUtils.copy(bytes, uploadedFile);
                System.out.println("********************************");
                System.out.println(uploadedFile.getAbsolutePath());
                System.out.println(bytes.length);
                System.out.println("********************************");
               
         
            return new ModelAndView(getSuccessView() + ".jsp");
        }

        protected void initBinder(HttpServletRequest request,
                ServletRequestDataBinder binder) throws ServletException {
            binder.registerCustomEditor(byte[].class,
                    new ByteArrayMultipartFileEditor());
        }
        public void setUploadDir(String uploadDir){
            this.uploadDir = uploadDir;
        }
        public String getUploadDir(){
            return this.uploadDir;
        }
    }
    <--------------------------控制類------------------------->
    <---------------------定義commandClass-------------------->
    public class FileUploadBean {

        private byte[] file;

        public void setFile(byte[] file) {
            this.file = file;
        }

        public byte[] getFile() {
            return file;
        }

    }
    <---------------------定義commandClass-------------------->
    4:定義一個form表單index.jsp
    <form method="post" action="upload.htm" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit"/>
    5:定義confirmation.jsp及fail.jsp
    confirmation.jsp如下:
    <%@ page contentType="text/html; charset=GBK" %>
    <html>
    <head>
    <title>
    successView
    </title>
    </head>
    <body bgcolor="#ffffff">
    <h1>
    Upload Successful
    </h1>
    </body>
    </html>

    fail.jsp如下:
    <html>
    <head>
    <title>Upload a file please</title>
    </head>
    <body>
    <h1>no file,Please upload a file</h1>
    <form method="post" action="uploadfile.htm" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit"/>
    </form>
    </body>
    </html>
    6:運行tomcat。
    預覽 ie里面:http://localhost/springmvc/index.jsp
    注:
    a:文件目錄為tomcat-HOME/webapps/springmvc/
    .jsp文件都放在根目錄下,.class文件放在/WEB-INF/classes/中

    其他文件放在/WEB-INF/里面。
    b:如果連上面的你都有疑問,那還是去看看spring的基礎知識吧。

    posted on 2005-09-02 11:04 撲撲 閱讀(27960) 評論(1)  編輯  收藏 所屬分類: Spring

    評論

    # re: 使用spring MVC框架進行文件上傳 2006-01-11 09:31 yeafee
    從別人的房間拿來的,就該寫在源來之處.  回復  更多評論
      

    主站蜘蛛池模板: 777成影片免费观看| 中文在线观看永久免费| 日本免费xxxx色视频| 五月天网站亚洲小说| 免费A级毛片无码A∨中文字幕下载| 国产亚洲人成网站在线观看| 国产免费福利体检区久久| 在线精品亚洲一区二区三区| 久久九九久精品国产免费直播| 国产国拍精品亚洲AV片| 天堂在线免费观看| 久久久久无码精品亚洲日韩| 99精品免费观看| 亚洲一区二区三区在线观看蜜桃| 无码人妻一区二区三区免费手机 | 亚洲AV无码片一区二区三区| 青青青青青青久久久免费观看| 亚洲精品无码一区二区 | 精品国产精品久久一区免费式| 亚洲国产美女精品久久久| 免费一级毛片在播放视频| 男女一边桶一边摸一边脱视频免费| 亚洲乱码精品久久久久..| 51精品视频免费国产专区| 亚洲午夜无码毛片av久久京东热| 四虎影视在线永久免费观看| 一个人看的www免费在线视频| 亚洲av永久无码精品国产精品| 久热中文字幕在线精品免费| 亚洲国产综合AV在线观看| 久久久久亚洲AV成人网人人网站| 暖暖在线视频免费视频| 亚洲一级毛片免观看| 免费永久看黄在线观看app| 一个人免费观看日本www视频| 久久水蜜桃亚洲av无码精品麻豆| 午夜免费不卡毛片完整版| 一级特黄录像免费播放肥| 亚洲导航深夜福利| 亚洲精品人成无码中文毛片| 99xxoo视频在线永久免费观看|