我尝试使用REST API上传文件(我使用的是wildfly服务器),我收到此错误:
无法执行:javax.ws.rs.NotSupportedException:找不到类型的消息正文阅读器:class com.sun.jersey.core.header.FormDataContentDisposition of content type:multipart / form-data;
这是我的代码:
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.services.DocumentFacadeREST.class);
}
}
@Stateless
@Path("documents")
public class DocumentFacadeREST{
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "E://uploadFileRest/"+ fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return output;
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我在pom.xml中使用的依赖项:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-core</artifactId>
<version>snap-r9548</version>
</dependency>
我的HTML格式:
<form action="webresources/documents/upload2" method="post" enctype="multipart/form-data">
<p>
Select a file :
<input type="file" name="file" size="45" />
</p>
<input type="submit" value="Upload" />
</form>
请你能帮助我知道我为什么会收到错误,我整天都在调查中没有任何结果。
提前致谢。
答案 0 :(得分:0)
尝试在野生动物中使用resteasyaxrs而不是jersy
需要的罐子
休息Api
@Path("/uploadfile")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(MultipartFormDataInput input) {
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> inputParts = uploadForm.get("file");
}