【发布时间】:2018-04-17 19:34:19
【问题描述】:
使用 test.html 上传文件时,我收到 HTTP 415 错误,媒体类型不受支持。球衣中的多部分似乎无法正常工作。在我的服务器控制台中,我收到以下错误:
No message body reader has been found for class org.glassfish.jersey.media.multipart.FormDataContentDisposition,
ContentType: multipart/form-data;boundary=----WebKitFormBoundaryFwksqvOnuCUBmh87
在对网络和其他 StackOverflow 文章进行了一些研究之后,我找不到合适的解决方案。我看到的反复出现的主题之一是关于注册 Jersey 多部分功能,但我不清楚在我的文件结构中的何处或如何执行此操作。我想我已经接近一个可行的解决方案,但还没有。这是我目前的代码:
项目结构
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.box</groupId>
<artifactId>box-java-sdk</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
上传文件.java
package wasdev.sample.rest;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
@ApplicationPath("api")
@Path("/upload")
public class UploadFiles{
@POST
@Path("/")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
String filename = fileMetaData.getFileName();
String UPLOAD_PATH = "\\home\\andy" + filename;
try
{
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out = new FileOutputStream(new File(UPLOAD_PATH));
while ((read = fileInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e)
{
throw new WebApplicationException("Error while uploading file. Please try again !!");
}
return Response.ok("Data uploaded successfully !!").build();
}
}
Test.html
<html>
<body>
<h1>File Upload Example - howtodoinjava.com</h1>
<form action="http://localhost:9080/GetStartedJava/api/upload" method="post" enctype="multipart/form-data">
<p>Select a file : <input type="file" name="file" size="45" accept=".jpg" /></p>
<input type="submit" value="Upload PDF" />
</form>
</body>
</html>
【问题讨论】: