你看过很多关于此的帖子,但我找不到我的问题。
我保持错误400
这是我的代码
POST
@Path("/upload")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFile( @FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
System.out.println(fileMetaData.getFileName());
return Response.ok("File caught successfully !!").build();
}
以下是我的泽西岛依赖
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.18</version>
</dependency>
我已将此添加到AppConfiguration
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
resources.add(MultiPartFeature.class);
return resources;
}
平心而论,它可能是我的实际HTTP请求,所以这是我的POSTman
答案 0 :(得分:0)
//您能否使用此代码上传多个文件
@Path("/files")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFiles2(@DefaultValue("") @FormDataParam("tags") String tags,
@FormDataParam("files") List<FormDataBodyPart> bodyParts,
@FormDataParam("files") FormDataContentDisposition fileDispositions,
@FormDataParam("file2") InputStream file2,
@FormDataParam("file2") FormDataContentDisposition fileDisposition2) {
StringBuffer fileDetails = new StringBuffer("");
/* Save multiple files */
for (int i = 0; i < bodyParts.size(); i++) {
/*
* Casting FormDataBodyPart to BodyPartEntity, which can give us
* InputStream for uploaded file
*/
BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyParts.get(i).getEntity();
String fileName = bodyParts.get(i).getContentDisposition().getFileName();
saveFile(bodyPartEntity.getInputStream(), fileName);
fileDetails.append(" File saved at /Volumes/Drive2/temp/file/" + fileName);
}
/* Save File 2 */
String file2Name = fileDisposition2.getFileName();
saveFile(file2, file2Name);
fileDetails.append(" File saved at /Volumes/Drive2/temp/file/" + file2Name);
fileDetails.append(" Tag Details : " + tags);
System.out.println(fileDetails);
HTML代码位于
之下<form action="/upload/files" enctype="multipart/form-data" method="post">
<label>Select multiple files</label><input type= "file" name="files" multiple /> <br/><br/>
<label>Select File</label><input type= "file" name="file2" /> <br/><br/>
<label>Tags</label> <input name="tags" maxlength="10"/> <br/><br/>
<input type="submit" title="Save"/>
</form>
答案 1 :(得分:0)
@Consumes
,并从邮递员中删除内容类型。它对我有用。
不要担心泽西岛会将其视为multipart form-data