Grails 4,Ubuntu 20,嵌入式 Tomcat - 请求中没有多部分文件
Grails 4, Ubuntu 20, Embedded Tomcat - No Multipart files in request
我已经尝试用 grails 4 应用程序的生产环境部署解决问题大约一周了。当我在本地 运行 我的应用程序时,通过 运行-app 或 java -jar warfile.war,我能够上传文件并在我的控制器中提供多部分内容。当我在 lightsail ubuntu 20 服务器上部署 运行 应用程序时,由于某些原因,这些部件根本不存在。
UI 表单代码:
<form id="file-form-${attrs.id}" enctype="multipart/form-data">
<input style="display:none;" id="${attrs.id}-file" type="file" name="file"
accept="image/png, image/jpeg, image/tiff"/>
</form>
UIajax/js代码:
var jForm = new FormData($('#file-form-'+id)[0]);
jForm.append("uploadField", $("#file-form-"+id).find('input[type="file"]').get(0).files[0]);
$.ajax({
url: '/files/uploadImage',
type: "POST",
data: jForm,
enctype: 'multipart/form-data',
processData: false,
cache: false,
contentType: false
}).done(function(data) {
// ...
}).fail(function(jqXHR, textStatus) {
// ...
});
控制器(请注意,所有日志语句都在这里是为了清楚起见,它写得非常丑陋,一旦清晰起来就会被清理):
def uploadImage() {
try {
log.debug("FilesController.uploadImage: IN for user ${session.user?.id}")
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest rqst = (MultipartHttpServletRequest) request
def myFile = params['uploadField']
params.each { k, v ->
log.debug("params k ${k}, v ${v}")
}
request.requestHeaders.each {k, v ->
log.debug("headers k ${k}, v ${v}")
}
def f
log.debug("parts " + request.parts?.size())
request.parts.each {part ->
log.debug("part ${part.name}")
if (f == null) f = part
}
log.debug("fileNames: ${request.getFileNames()}")
request.getFileNames().each { String fileName ->
log.debug("fileName: ${fileName}")
}
request.fileMap.each { String name, MultipartFile file ->
log.debug("Looping fileMap: name: ${name} and file ${file}")
if (f == null) f = file
}
log.debug('file: ' + f)
if (f == null) f = myFile
log.debug('file: ' + f)
log.debug("FilesController.uploadImage: calling imageService for user ${session.user?.id} ")
log.debug("imageService " + imageService)
File file = imageService.saveImage(f, session.user)
render text: file?.id
return
}
} catch (Exception e) {
log.error("FilesController.uploadImage: error ${e.getMessage()}")
}
render text: ''
}
日志(在产品中):2020-12-07 18:11:51.977 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : FilesController.uploadImage: IN for user uuid-xxxxx 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k controller, v files 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k format, v null 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k action, v uploadImage 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k host, v [xxx.com] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k connection, v [keep-alive] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k content-length, v [3477461] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept, v [*/*] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k x-requested-with, v [XMLHttpRequest] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k user-agent, v [Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k content-type, v [multipart/form-data; boundary=----WebKitFormBoundaryNqkRdP2KDJ3aJYMb] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k origin, v [https://example.com] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-site, v [same-origin] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-mode, v [cors] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-dest, v [empty] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k referer, v [https://example.com/toolsList/list?toolSetID=2c9a8202763005520176300c0254001d] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept-encoding, v [gzip, deflate, br] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept-language, v [en-US,en;q=0.9] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k cookie, v [pvisitor=ff1209b4-edb0-4246-b877-8e42fe356908; JSESSIONID=C8C338FA7AC3DA7F0A967492864939BF] 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : parts 0 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : fileNames: java.util.LinkedHashMap$LinkedKeyIterator@5f7cb675 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : file: null 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : file: null 2020-12-07 18:11:51.984 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : FilesController.uploadImage: calling imageService for user uuid-xxx 2020-12-07 18:11:51.984 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : imageService xxx.files.ImageService@5c93194d 2020-12-07 18:11:51.985 INFO --- [nio-8443-exec-2] xxx.files.ImageService : FilesService.saveImage: IN for uuid-xxx named null 2020-
在本地,那里有一个文件。
在我的 application.yml:
grails:
disableCommonsMultipart: false
web:
disable:
multipart: false
我不知道为什么在我的本地环境中接收到文件内容,但在生产环境中却接收不到。我无法想象 aws lightsail 会从 https 请求中剥离内容。在本地,我使用的是 http 而不是 https,不确定为什么会很重要。
有什么想法吗?预先感谢您提供的任何见解。
最终,问题证明是ssl相关的。我使用的 grails 版本是 4.0.3。对于该版本,嵌入的 tomcat 依赖项是 org.springframework.boo:spring-boot-starter-tomcat 并且被拉取的版本是 2.1.13.RELEASE。 spring 启动库利用了 tomcat 版本 9.0.31。找到这篇文章 后,只需指定一个 tomcat 版本即可解决在 ssl 上解析多部分文件的问题:
compile "org.apache.tomcat.embed:tomcat-embed-websocket:9.0.33"
compile "org.apache.tomcat.embed:tomcat-embed-core:9.0.33"
compile "org.apache.tomcat.embed:tomcat-embed-el:9.0.33"
compile "org.apache.tomcat:tomcat-juli:9.0.33"
compile "org.apache.tomcat:tomcat-annotations-api:9.0.33"
我希望它能为其他人节省一些时间。和平
我已经尝试用 grails 4 应用程序的生产环境部署解决问题大约一周了。当我在本地 运行 我的应用程序时,通过 运行-app 或 java -jar warfile.war,我能够上传文件并在我的控制器中提供多部分内容。当我在 lightsail ubuntu 20 服务器上部署 运行 应用程序时,由于某些原因,这些部件根本不存在。
UI 表单代码:
<form id="file-form-${attrs.id}" enctype="multipart/form-data">
<input style="display:none;" id="${attrs.id}-file" type="file" name="file"
accept="image/png, image/jpeg, image/tiff"/>
</form>
UIajax/js代码:
var jForm = new FormData($('#file-form-'+id)[0]);
jForm.append("uploadField", $("#file-form-"+id).find('input[type="file"]').get(0).files[0]);
$.ajax({
url: '/files/uploadImage',
type: "POST",
data: jForm,
enctype: 'multipart/form-data',
processData: false,
cache: false,
contentType: false
}).done(function(data) {
// ...
}).fail(function(jqXHR, textStatus) {
// ...
});
控制器(请注意,所有日志语句都在这里是为了清楚起见,它写得非常丑陋,一旦清晰起来就会被清理):
def uploadImage() {
try {
log.debug("FilesController.uploadImage: IN for user ${session.user?.id}")
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest rqst = (MultipartHttpServletRequest) request
def myFile = params['uploadField']
params.each { k, v ->
log.debug("params k ${k}, v ${v}")
}
request.requestHeaders.each {k, v ->
log.debug("headers k ${k}, v ${v}")
}
def f
log.debug("parts " + request.parts?.size())
request.parts.each {part ->
log.debug("part ${part.name}")
if (f == null) f = part
}
log.debug("fileNames: ${request.getFileNames()}")
request.getFileNames().each { String fileName ->
log.debug("fileName: ${fileName}")
}
request.fileMap.each { String name, MultipartFile file ->
log.debug("Looping fileMap: name: ${name} and file ${file}")
if (f == null) f = file
}
log.debug('file: ' + f)
if (f == null) f = myFile
log.debug('file: ' + f)
log.debug("FilesController.uploadImage: calling imageService for user ${session.user?.id} ")
log.debug("imageService " + imageService)
File file = imageService.saveImage(f, session.user)
render text: file?.id
return
}
} catch (Exception e) {
log.error("FilesController.uploadImage: error ${e.getMessage()}")
}
render text: ''
}
日志(在产品中):2020-12-07 18:11:51.977 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : FilesController.uploadImage: IN for user uuid-xxxxx 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k controller, v files 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k format, v null 2020-12-07 18:11:51.978 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : params k action, v uploadImage 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k host, v [xxx.com] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k connection, v [keep-alive] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k content-length, v [3477461] 2020-12-07 18:11:51.980 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept, v [*/*] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k x-requested-with, v [XMLHttpRequest] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k user-agent, v [Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k content-type, v [multipart/form-data; boundary=----WebKitFormBoundaryNqkRdP2KDJ3aJYMb] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k origin, v [https://example.com] 2020-12-07 18:11:51.981 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-site, v [same-origin] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-mode, v [cors] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k sec-fetch-dest, v [empty] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k referer, v [https://example.com/toolsList/list?toolSetID=2c9a8202763005520176300c0254001d] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept-encoding, v [gzip, deflate, br] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k accept-language, v [en-US,en;q=0.9] 2020-12-07 18:11:51.982 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : headers k cookie, v [pvisitor=ff1209b4-edb0-4246-b877-8e42fe356908; JSESSIONID=C8C338FA7AC3DA7F0A967492864939BF] 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : parts 0 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : fileNames: java.util.LinkedHashMap$LinkedKeyIterator@5f7cb675 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : file: null 2020-12-07 18:11:51.983 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : file: null 2020-12-07 18:11:51.984 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : FilesController.uploadImage: calling imageService for user uuid-xxx 2020-12-07 18:11:51.984 DEBUG --- [nio-8443-exec-2] xxx.files.FilesController : imageService xxx.files.ImageService@5c93194d 2020-12-07 18:11:51.985 INFO --- [nio-8443-exec-2] xxx.files.ImageService : FilesService.saveImage: IN for uuid-xxx named null 2020-
在本地,那里有一个文件。
在我的 application.yml:
grails:
disableCommonsMultipart: false
web:
disable:
multipart: false
我不知道为什么在我的本地环境中接收到文件内容,但在生产环境中却接收不到。我无法想象 aws lightsail 会从 https 请求中剥离内容。在本地,我使用的是 http 而不是 https,不确定为什么会很重要。
有什么想法吗?预先感谢您提供的任何见解。
最终,问题证明是ssl相关的。我使用的 grails 版本是 4.0.3。对于该版本,嵌入的 tomcat 依赖项是 org.springframework.boo:spring-boot-starter-tomcat 并且被拉取的版本是 2.1.13.RELEASE。 spring 启动库利用了 tomcat 版本 9.0.31。找到这篇文章
compile "org.apache.tomcat.embed:tomcat-embed-websocket:9.0.33"
compile "org.apache.tomcat.embed:tomcat-embed-core:9.0.33"
compile "org.apache.tomcat.embed:tomcat-embed-el:9.0.33"
compile "org.apache.tomcat:tomcat-juli:9.0.33"
compile "org.apache.tomcat:tomcat-annotations-api:9.0.33"
我希望它能为其他人节省一些时间。和平