在 Spring Roo 2.0 中,如何使用 multipart/form-data 发送二进制文件

In Spring Roo 2.0, how can I send a binary file using multipart/form-data

我有一个 class 需要存储二进制文件,所以我有文件类型的字段:

entity jpa --class ~.model.Attachment
field string --fieldName description
field string --fieldName createdBy
field enum --fieldName attachmentType –type ~.model.attachmentTypesEnum
field string --fieldName fileName
field file --fieldName attachmentFilei --contentType ZIP --autoUpload --notNull false

现在我需要让我的移动应用程序上传一个二进制文件。我意识到我可以使用 Base64 编码将其转换为可打印的字符串,但文件可能非常大,所以我更愿意使用 multipart/form-data 发送二进制数据。我不需要存储在 mysql 数据库中的二进制文件,因为我宁愿将它放在 AWS 的 S3 存储桶中。因此,我可以单独发送二进制文件,return 将其位置发送给客户端,然后客户端可以将附件记录作为 JSON 引用 S3 中的位置。

field string --fieldName bucketId
field string --fieldName S3URL

我发现很多关于此主题的讨论都建议使用 Jersey 发送文件。但是当我尝试编译时,我的 Spring Roo 项目中缺少该代码会使 Maven 崩溃。我认为它不喜欢 @Path 行。

ERROR] execute error
java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at com.mysema.maven.apt.AbstractProcessorMojo.execute(AbstractProcessorMojo.java:346)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at com.sun.tools.javac.main.Main.compile(Main.java:469)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:132)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at com.querydsl.codegen.SimpleSerializerConfig.getConfig(SimpleSerializerConfig.java:30)
at com.querydsl.apt.DefaultConfiguration.<init>(DefaultConfiguration.java:112)
at com.querydsl.apt.jpa.JPAConfiguration.<init>(JPAConfiguration.java:54)
at org.springframework.roo.querydsl.processor.RooAnnotationProcessor.createConfiguration(RooAnnotationProcessor.java:37)
at com.querydsl.apt.AbstractQuerydslProcessor.process(AbstractQuerydslProcessor.java:79)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:793)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:722)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access00(JavacProcessingEnvironment.java:97)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1029)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1163)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1108)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:824)
at com.sun.tools.javac.main.Main.compile(Main.java:439)
... 6 more
[INFO] -----------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

你能给我指点任何用于将二进制文件发送到我的 Spring Roo 项目的文档或示例代码吗?

因为我想将文件存储在 AWS S3 中并且不想对它们进行 base 64 编码,所以我在 http://www.mkyong.com/spring-boot/spring-boot-file-upload-example/ 上找到了一个关于上传二进制文件的非常好的答案。它运行良好,我使用 JSON.

将有关文件的信息传递给附件 class