posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉(zhuǎn)摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 866057
          • 排名 - 44

          最新評論

          閱讀排行榜

          在grails中實現(xiàn)上傳文件也很簡單。它可以使用spring里面的CommonsMultipartFile類來處理上傳文件.
          可以對文件的一些屬性設(shè)置,比如大小:
          <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="maxUploadSize">
          <value>1000000</value>
          </property>
          </bean>
          當(dāng)然gsp頁面需要在form里面設(shè)置enctype="multipart/form-data"
          <g:form method="post" action="save" enctype="multipart/form-data">
          <input type="file" name="file"/>
          <input type="submit"/>
          </g:form>

          下面就是處理上傳的文件了:

          import org.springframework.web.multipart.MultipartHttpServletRequest
          import org.springframework.web.multipart.commons.CommonsMultipartFile

          class UploadController {
              static String uploadDir = "uploadfile"
              def index = {
                  render(view:"upload")
              }
              def save = {
                  if (request instanceof MultipartHttpServletRequest) {
                      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request
                      CommonsMultipartFile orginalFile = (CommonsMultipartFile) multiRequest.getFile("file")
                      // 判斷是否上傳文件
                      if (orginalFile != null && !orginalFile.isEmpty()) {
                          // 獲取系統(tǒng)默認(rèn)文件路徑分隔符
                          def separator = System.getProperty("file.separator")
                          println "file separator is ${separator} "
                          // 獲取原文件名稱
                          String originalFilename = orginalFile.getOriginalFilename()
                          // 獲取上傳文件擴(kuò)展名
                          def extension = originalFilename.substring(originalFilename.indexOf(".") + 1)
                          println "extension is ${extension}"
                          def name = ".." + separator + uploadDir + separator + orginalFile.getOriginalFilename()
                          println "file name is : ${name}"
                          // 使用存放文件的絕對路徑創(chuàng)建輸出流
                           /**
                          DataOutputStream out = new DataOutputStream(new FileOutputStream(name))
                          InputStream is = null
                          try {
                              is = orginalFile.getInputStream()
                              byte[] buffer = new byte[1024]
                              while (is.read(buffer) > 0) {
                                out.write(buffer) // 寫入磁盤
                              }
                          } catch (IOException exception) {
                              exception.printStackTrace()
                          } finally {
                              if (is != null) {
                                  is.close()
                              }
                              if (out != null) {
                                  out.close()
                              }
                          }
                          */
                          orginalFile.transferTo(new File(name))
                          render(view:"success")
                      }
                    
                  } else {
                      println "No multipart"
                  }
              }
          }


          posted on 2008-06-04 23:50 周銳 閱讀(3126) 評論(1)  編輯  收藏 所屬分類: Groovy&Grails
          主站蜘蛛池模板: 涡阳县| 承德县| 黄山市| 宁明县| 金溪县| 深州市| 高州市| 尼木县| 来凤县| 泸西县| 喀什市| 库尔勒市| 荥经县| 岳池县| 合阳县| 江达县| 南江县| 镇平县| 天长市| 东阳市| 兰溪市| 高唐县| 石楼县| 西贡区| 江口县| 梁平县| 汾西县| 台南市| 张家界市| 娱乐| 安远县| 拜城县| 赤峰市| 丰顺县| 泾阳县| 涟源市| 枣阳市| 武鸣县| 东兰县| 九龙坡区| 科技|