我正在开发一个托管在 Liberty 服务器上的 REST 应用程序,该应用程序应该允许支持文件上传。作为此 POST 请求的一部分,我希望允许用户使用表单参数指定一些内容。尽管在这里使用了 IBM 的示例,但我还是无法让它发挥作用。我可以使用标题检索与上传文件相关的信息(如示例中所示)。但是当我尝试使用相同的方法来检索有关标头的信息时,唯一可用的是字段的名称,而不是它们的值。
我也在努力保持对 Swagger 的支持。
这是我的代码片段:
@ApiOperation("Upload Source Code to Build")
@POST
@Path("/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.MULTIPART_FORM_DATA)
@ApiResponses({
@ApiResponse(code = 200, message= "OK", response=String.class)})
@ApiImplicitParams({
@ApiImplicitParam(
name="source",
value = "Source File",
required = true,
dataType = "java.io.File",
paramType = "form",
type="file"),
@ApiImplicitParam(
name="archive_name",
value = "Name of File to Retrieve",
required = true,
dataType = "java.lang.String",
paramType = "form",
type="string"),
@ApiImplicitParam(
name="build_command",
value = "Command to Run",
required = true,
dataType = "java.lang.String",
paramType = "form",
type="string")})
public Response uploadFileBuildArchive(
@ApiParam(hidden=true) IMultipartBody multipartBody,
@Context UriInfo uriInfo) throws IOException {
List<IAttachment> attachments = multipartBody.getAllAttachments();
InputStream stream = null;
Iterator<IAttachment> it = attachments.iterator();
IAttachment attachment = it.next();
if (attachment == null)
return Response.status(400).entity("No file attached").build();
StringBuilder fileName = new StringBuilder("");
StringBuilder archiveName = new StringBuilder("");
StringBuilder buildCommand = new StringBuilder("");
for(int i = 0; it.hasNext(); i++) {
DataHandler dataHandler = attachment.getDataHandler();
MultivaluedMap<String, String> map = attachment.getHeaders();
String[] contentDisposition = map.getFirst("Content-Disposition").split(";");
if(i == 0) {
stream = dataHandler.getInputStream();
Response saveFileResponse = handleFileStream(contentDisposition, stream, fileName);
if (saveFileResponse != null)
return saveFileResponse;
} else if (i == 1) {
if (!handleFormParam(contentDisposition, archiveName))
return Response.status(400).entity("Missing FormParam: archive_name").build();
}
.
.
.
private boolean handleFormParam(String[] contentDisposition, StringBuilder stringBuilder) {
String formElementName = null;
for (String tempName : contentDisposition) {
String[] names = tempName.split("=");
for (String n : names) {
System.out.println(tempName + ":" + n);
}
}
if (stringBuilder.length() == 0) {
return false;
}
return true;
}
我可以从附件中获得的关于“archive_name”表单参数的唯一信息是参数的名称是“archive_name”。如何获取与“archive_name”关联的值?