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

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 865017
          • 排名 - 44

          最新評(píng)論

          閱讀排行榜

          在grails中實(shí)現(xiàn)上傳文件也很簡(jiǎn)單。它可以使用spring里面的CommonsMultipartFile類來處理上傳文件.
          可以對(duì)文件的一些屬性設(shè)置,比如大小:
          <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="maxUploadSize">
          <value>1000000</value>
          </property>
          </bean>
          當(dāng)然gsp頁(yè)面需要在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}"
                          // 使用存放文件的絕對(duì)路徑創(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 周銳 閱讀(3121) 評(píng)論(1)  編輯  收藏 所屬分類: Groovy&Grails
          主站蜘蛛池模板: 临泽县| 遂溪县| 绍兴县| 上虞市| 迁西县| 广德县| 林口县| 巫山县| 昌宁县| 重庆市| 台湾省| 新邵县| 巨野县| 高邮市| 普陀区| 和硕县| 西安市| 田东县| 岱山县| 恭城| 扎囊县| 江陵县| 南开区| 武鸣县| 邢台市| 平南县| 固安县| 聊城市| 会理县| 芮城县| 长泰县| 邵东县| 石渠县| 福建省| 原阳县| 南澳县| 随州市| 桂阳县| 息烽县| 连平县| 九江市|