java - 无法使用球衣 2 上传文件。错误 400
问题描述
一开始我可以用这个 curl 请求上传我的文件:
curl -u "admin:admin" -X POST " http://myurlu:9086/service/rest/v1/components?repository=ebooks-store " -H "accept: application/json" -H "Content-Type:多部分/表单数据”-F“raw.directory=test”-F“raw.asset1=@billet.pdf;type=application/pdf”-F“raw.asset1.filename=billet.pdf”
并使用成功的日志:
2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 使用参数上传组件:repository="ebooks-store" format="raw" directory="test" 2019-08-27 17:27:40,734+0000 INFO [qtp969575574-10357] admin org.sonatype.nexus.repository.upload.internal.UploadManagerImpl - 带有参数的资产:file="billet.pdf" 文件名="方坯.pdf"
我有在我的代码中上传的用例,所以我使用 jersey 创建了这个服务:
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:\\Users\\tkossoko\\Documents\\article.pdf"),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
.field("raw.directory", "test")
.field("raw.asset1.filename.", "article.pdf")
.bodyPart(filePart);
String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
webTarget = client.target(url);
//Very important to do, we have to register this
webTarget.register(MultiPartFeature.class);
final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
.post(Entity.entity(multipart, multipart.getMediaType()));
//Use response object to verify upload success
formDataMultiPart.close();
multipart.close();
但我每次都得到这个错误:
message='在'1'上缺少必需的资产字段'文件名'}]'; 映射自:org.sonatype.nexus.rest.ValidationErrorsException:未知组件字段“asset1.filename。”,缺少必需的资产字段“1”上的“文件名”
我的代码有什么问题?我使用的网络服务正在等待 3 个参数:
raw.directory 字符串 raw.assetN 文件 raw.assetN.filename 字符串
解决方案
正确答案是
FileDataBodyPart filePart = new FileDataBodyPart("raw.asset1", new File("C:\\Users\\tkos\\Documents\\article.pdf"),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
.field("raw.directory", "test")
.field("raw.asset1.filename.", "article.pdf")
.bodyPart(filePart);
String url = nexusBaseUrl+"v1/components?repository="+repositoryName;
webTarget = client.target(url);
//Very important to do, we have to register this
webTarget.register(MultiPartFeature.class);
final Response response = webTarget.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON).header("Expect", "100-continue")
.post(Entity.entity(multipart, multipart.getMediaType()));
//Use response object to verify upload success
formDataMultiPart.close();
multipart.close();
推荐阅读
- c# - C# 使用在具体类声明中引用自身的泛型
- google-sheets - 为什么 =Importrange() 会在 Google 表格中导致 #name 错误?
- jquery - Does Bootstrap version 3.4 compatible with JQuery version 3.4?
- c# - 如何使用 PostgreSQL 获得类似 LINQ-to-SQL 的功能?
- react-native - 如何将 Shopify 应用程序集成到 React Native 中
- python - Tkinter 获取图像以跟随光标与先前的移动
- java - 将一个简单的 Java 库发布到 Maven
- .net-core - 应用程序依赖项清单中指定的程序集(
.deps.json) 未找到 - c# - API 调用的间歇性连接错误,直到手动回收 IIS 应用程序池
- git - 克隆存储库时如何获取命令行以重新提示 github 的用户名/密码