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

如何使用grails将文件上载到服务器目录?,grails,Grails,如何将文件上载到服务器目录。。 如果我的项目位于D:\myapp,并且我使用cmd D:\myapp grails run app运行 当我运行此应用程序和其他计算机运行它并上载文件时..它会将ini计算机服务器保存在目录D:\myapp\upload 我试着用这个 <g:form action="list" enctype="multipart/form-data" useToken="true"> <span class="button"> &

如何将文件上载到服务器目录。。 如果我的项目位于D:\myapp,并且我使用cmd D:\myapp grails run app运行 当我运行此应用程序和其他计算机运行它并上载文件时..它会将ini计算机服务器保存在目录D:\myapp\upload

我试着用这个

<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')
}

Grails应用程序的位置并不重要。您必须在控制器中指定完整的目标路径。这里有一个例子

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

    f.transferTo(new File('D:\myapp\upload\file_name.txt')) 
    response.sendError(200, 'Done') 
}

目标只是一个类似于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)
如果要将其存储在相对于用户主页的某个路径上:

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)
更新 根据您的
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>

应该是:

<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>

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

最终字符串IMAGE_DIR=“${servletContext.getRealPath('/images')}/”


“D:\myapp\upload\file_name.txt”这是一个绝对路径?如果myapp在E:\myapp中如何?groovy.lang.MissingMethodException:没有方法签名:org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile()适用于参数类型:(java.lang.String)值:[filecsv]可能的解决方案:getXML(),getAt(java.lang.String)、getAt(java.lang.String)、getLocale()、getJSON()、getHeader(java.lang.String)@unekwu把上传的文件放到webroot是个坏主意,它会在应用程序重启时被完全删除。不管怎样,问题是关于
getFile
@unekwu它实际上取决于容器,但是Tomcat,例如,会在每个WAR解包上删除应用程序目录(当然,如果你使用WARs)@unekwu 2014-02-03 14:11:11034[http-8080-1]ERROR errors.grailExceptionResolver-没有这样的属性:类的参数:com.teravin.wallet.LoanAccountController org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack:没有这样的属性:类的参数:com.teravin.wallet.LoanAccountController为什么会这样?images/some\u文件夹"我必须先创建这个文件夹,否则它会自动创建它?对不起,我是一个初学者,谢谢你,也谢谢@IgorArtamonov。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题会真的有助于提高你的文章质量,并可能导致更多的投票。记住,你正在回答这个问题对于未来的读者,不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
<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>
    def employeeId = "dablu_photo";

    def employeePicture = request.getFile("cv_");

    String photoUrl  ="";
    if (employeePicture && !employeePicture.empty) {
        if (new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png")?.exists()){
            FileUtils.deleteQuietly(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"));
        }
        employeePicture.transferTo(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"))

    }