waterye

          Spring集成FileUpload

          1. project-servlet.xml
          <!-- MultipartResolver for parsing file uploads, implementation for Commons FileUpload -->
          <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                  
          <!-- one of the properties available; the maximum file size in bytes -->
                  
          <property name="maxUploadSize">
                      
          <value>2000000</value>
                  
          </property>
          </bean>

          2. upload.html
          <form action="" method="post" encType="multipart/form-data">
          <input type="file" name="f">
          <input type="submit">
          </form>

          3. Controller.java
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile 
          = multipartRequest.getFile("f");
            private final String uploadDir= "uploadDir";
            String filePathName 
          = getServletContext().getRealPath(File.separator + uploadDir);
            File filePath 
          = new File(filePathName);
            
          if (!filePath.exists()) { filePath.mkdirs(); }
            String fileName 
          = filePathName + File.separator + multipartFile.getOriginalFilename();
            File file 
          = new File(fileName);
            multipartFile.transferTo(file);

          參考:
          1. spring's sample: imagedb
          2. spring reference

          posted on 2006-01-15 22:46 waterye 閱讀(3083) 評論(1)  編輯  收藏 所屬分類: spring

          Feedback

          # re: Spring集成FileUpload 2008-01-05 14:54 234

          432  回復  更多評論   

          主站蜘蛛池模板: 龙山县| 大竹县| 吴桥县| 临沧市| 和平区| 濉溪县| 伊宁县| 南江县| 上杭县| 陇西县| 唐山市| 黄山市| 镇原县| 绥棱县| 浦江县| 连江县| 平顶山市| 禄丰县| 博湖县| 崇文区| 杂多县| 德化县| 高淳县| 搜索| 巴林右旗| 兴海县| 泌阳县| 宣威市| 丘北县| 察雅县| 诏安县| 祥云县| 天台县| 定日县| 孙吴县| 安义县| 腾冲县| 囊谦县| 灵寿县| 和顺县| 余姚市|