如何使用grails将文件上传到服务器目录?

4 grails

如何将文件上传到服务器目录..如果我的项目在D:\ myapp和我运行cmd d:\ myapp grails run-app当我运行这个应用程序和其他计算机运行它并上传文件..将保存ini计算机目录D:\ myapp\upload?中的服务器?

我试试这个ini gsp.

<g:form action="list" enctype="multipart/form-data" useToken="true">
    <span class="button">
        <input type="file" name="filecsv"/>
        <input type="button" class="upload" value="Upload"
               onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>
    </span>
</g:form>

def upload = {

    def f = request.getFile('filecsv')
    if (f.empty) {
        flash.message = 'file cannot be empty'
        render(view: 'list')
        return
    }

    f.transferTo(new File('C:\Users\meta\Documents\workspace-sts-2.5.2.RELEASE\wawet\wallet\uploads\file_name.csv'))
    response.sendError(200, 'Done')
}
Run Code Online (Sandbox Code Playgroud)

这是错误:

2014-02-03 10:43:02,706 [http-8080-2] ERROR errors.GrailsExceptionResolver  - No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
groovy.lang.MissingMethodException: No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
        at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController:308)
        at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController)
        at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)

Emm*_*ohn 16

目标只是Java中的文件.

def f = request.getFile('some_file')

//validate file or do something crazy hehehe

//now transfer file
File fileDest = new File("Path to some destination and file name")
f.transferTo(fileDest)
Run Code Online (Sandbox Code Playgroud)

或者,如果要将其存储在相对于用户主目录的某个路径中:

def homeDir = new File(System.getProperty("user.home")) //user home e.g /home/username for unix
File fileDest = new File(homeDir,"path/to/some_folder")
f.transferTo(fileDest)
Run Code Online (Sandbox Code Playgroud)

更新 根据您getFile的工作原因,您没有提交表格:

<g:form action="list" enctype="multipart/form-data" useToken="true">

<span class="button">                   
                    <input type="file" name="filecsv"/>
                    <input type="button" class="upload"
                                        value="Upload"
                                        onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>

            </span>

</g:form>
Run Code Online (Sandbox Code Playgroud)

应该:

<g:form action="upload" enctype="multipart/form-data" useToken="true">

<span class="button">                   
                    <input type="file" name="filecsv"/>
                    <input type="submit" class="upload" value="upload"/>

            </span>

</g:form>
Run Code Online (Sandbox Code Playgroud)

如果您需要使用javascript,则应提交表单,而不是添加指向其他页面的链接.

  • groovy.lang.MissingMethodException:没有方法签名:org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile()适用于参数类型:(java.lang.String)values:[filecsv]可能的解决方案:getXML() ,getAt(java.lang.String),getAt(java.lang.String),getLocale(),getJSON(),getHeader(java.lang.String) (2认同)