gpt4 book ai didi

rest - 无法通过GRAILS中的REST请求上传文件

转载 作者:行者123 更新时间:2023-12-02 16:00:48 24 4
gpt4 key购买 nike

我正在尝试使用GRAILS通过REST上传文件

curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: d5d7aef8-3964-311b-8b64-4a7a82c52323" -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "file1=myfile.jpg" -F "fname=swateek" -F "lname=jena" 'http://localhost:8081/sampleFileREST/document/upload'


这是我的 Controller 的外观:
class DocumentController extends RestfulController<Document> {

static responseFormats = ['json', 'xml']

def upload() {

def fileLocation="<xml>empty</xml>"
def file = request.getParameter('file1')
def f1 = request.getParameter('fname')
def f2 = "<abc>"+request.getParameter('lname')+"</abc>"
def params = "gf"
if(file.empty) {
fileLocation = "<xml>"+"File cannot be empty"+"</xml><allprm>"+params+"</allprm>"
} else {
def documentInstance = new Document()
documentInstance.filename = file.originalFilename
documentInstance.fullPath = grailsApplication.config.uploadFolder + documentInstance.filename
file.transferTo(new File(documentInstance.fullPath))
documentInstance.save()
fileLocation = "<xml>"+documentInstance.fullPath+"</xml>"
}
/* return "File uploaded to: "+documentInstance.fullPath */

render(text: fileLocation, contentType: "text/xml", encoding: "UTF-8")
}

}

我可以访问请求的参数,除了我在请求中发送的文件以外的任何内容。

无法找出问题所在。

更新

我曾使用.getParameter()来获取文件。那是不正确的,正确的方法如下:
request.getFile('<filename>') // without the <>

这可能会在IntelliJ中引发“找不到符号”或“无法解决方法”错误,请按照以下答案中的步骤进行操作。

最佳答案

该死的我正在使用的IDE, IntelliJ

同样,这段代码在获取文件时:

def file = request.getParameter('file1')

应该替换为
def file = request.getFile('file1')

现在以前,当我使用request.getFile()方法时,出现“找不到符号”错误,并且无法执行请求。

解决方案:
  • 打开IntelliJ
  • 单击"file"
  • 找到选项“使缓存无效/重新启动”,然后等待IntelliJ重新出现。

  • 如果这不起作用,则在此答案中提及另一种方式:
    IntelliJ IDEA JDK configuration on Mac OS

    关于rest - 无法通过GRAILS中的REST请求上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308804/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com