本文介绍了如何使用grails将文件上传到服务器目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何将文件上传到服务器目录.. 如果我的项目位于D:\myapp并且我使用cmd运行d:\myapp grails run-app 当我运行此应用程序和其他电脑运行它并上传文件..它会保存ini电脑服务器目录D:\myapp\upload? 我试试这个ini gsp。 < g:form action =listenctype =multipart / form-datauseToken =true> < span class =button> < input type =filename =filecsv/> < input type =buttonclass =uploadvalue =Upload onclick ='location.href =$ {createLink(url:[action:'upload'])} /> < / span> < / g:表格> def upload = { def f = request.getFile('filecsv') if(f.empty){ flash.message = '文件不能为空' 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,'完成')} 这是错误: 2014-02-03 10:43:02,706 [http-8080-2] ERROR errors.GrailsExceptionResolver - 没有方法签名: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) groovy.lang.MissingMethodException:没有签名hod:org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile()适用于参数类型:(java.lang.String)values:[filecsv] 可能的解决方案:getXML(),getAt(java。 (String.lang.String),getAt(java.lang.String),getLocale(),getJSON(),getHeader(java.lang.String) at com.teravin.wallet.LoanAccountController $ _closure12.doCall(com.teravin .wallet.LoanAccountController:308)在com.teravin.wallet.LoanAccountController $ _closure12.doCall(com.teravin.wallet.LoanAccountController)$ b $在java.lang.Thread.run(Thread.java:744 ) 解决方案目的地只是像Java一样的文件。 def f = request.getFile('some_file') //验证文件或做某事疯狂hehehe //现在传输文件 def webrootDir = servletContext.getRealPath(/)// app目录文件fileDest = new File(webrootDir,images / some_folder ) f.transferTo(fileDest) 或 def homeDir = new File(System.getProperty(user.home))// user home eg / home / username for unix file fileDest = new File(homeDir,path / to / some_folder) f.transferTo(fileDest) 向下投票者的更新:假设您的应用具有适当的权限,您可以写入任何文件系统位置。 更新 为什么您的 getFile code>不工作,您没有提交表单: < g:form action =list enctype =multipart / form-datauseToken =true> < span class =button> < input type =filename =filecsv/> < input type =buttonclass =upload value =Upload onclick ='location.href =$ {createLink(url:[action:'upload' ])}'/> < / span> < / g:表格> 应该是: < g:form action =uploadenctype =multipart / form-datauseToken =true> < span class =button> < input type =filename =filecsv/> < input type =submitclass =uploadvalue =upload/> < / span> < / g:表格> 如果您需要使用JavaScript,您应该提交表单,而不是将链接添加到其他页面。 / p> how to upload file into server directory..if my project at D:\myapp and i run with cmd d:\myapp grails run-appwhen i run this app and other Computer run it and upload file..it will save ini computer server in directory D:\myapp\upload ?i try this 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')}this is the error : 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) 解决方案 The destination is only a file like in Java.def f = request.getFile('some_file')//validate file or do something crazy hehehe//now transfer filedef webrootDir = servletContext.getRealPath("/") //app directoryFile fileDest = new File(webrootDir,"images/some_folder")f.transferTo(fileDest)ORdef homeDir = new File(System.getProperty("user.home")) //user home e.g /home/username for unixFile fileDest = new File(homeDir,"path/to/some_folder")f.transferTo(fileDest)UPDATE for the down voters: You can write to any file system location assuming your app have the appropriate permissions.servletContext is available in every grails controller.UPDATEAs per why your getFile is not working, you are not submitting your form:<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>Should be: <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>if you need to use javascript you should submit the form instead of adding a link to another page. 这篇关于如何使用grails将文件上传到服务器目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 11:38