posts - 431,  comments - 344,  trackbacks - 0
          在grails中實現上傳文件也很簡單。它可以使用spring里面的CommonsMultipartFile類來處理上傳文件.
          可以對文件的一些屬性設置,比如大小:
          <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="maxUploadSize">
          <value>1000000</value>
          </property>
          </bean>
          當然gsp頁面需要在form里面設置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()) {
                          // 獲取系統默認文件路徑分隔符
                          def separator = System.getProperty("file.separator")
                          println "file separator is ${separator} "
                          // 獲取原文件名稱
                          String originalFilename = orginalFile.getOriginalFilename()
                          // 獲取上傳文件擴展名
                          def extension = originalFilename.substring(originalFilename.indexOf(".") + 1)
                          println "extension is ${extension}"
                          def name = ".." + separator + uploadDir + separator + orginalFile.getOriginalFilename()
                          println "file name is : ${name}"
                          // 使用存放文件的絕對路徑創建輸出流
                           /**
                          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 周銳 閱讀(3116) 評論(1)  編輯  收藏 所屬分類: Groovy&Grails
          主站蜘蛛池模板: 阳城县| 哈尔滨市| 中宁县| 东城区| 吐鲁番市| 大安市| 湄潭县| 收藏| 吉安县| 和田县| 屏东市| 泽州县| 阜新市| 开阳县| 永宁县| 千阳县| 江口县| 蒙山县| 平南县| 光泽县| 嵊州市| 保德县| 沧源| 吴旗县| 竹溪县| 永济市| 蒲江县| 太湖县| 泸水县| 驻马店市| 仪征市| 渝中区| 瑞昌市| 贵德县| 庆元县| 饶平县| 阜康市| 河间市| 正阳县| 平昌县| 南华县|