浏览 jboss的RESTEasy Guide 4.4.0-final。也有例子可以指导。
想要创建一个小型服务,您可以在其中发布带有一些元数据的二进制文件。在这种特殊情况下,我想发布一个文件+“该文件的所有者”。
在我的第一次尝试中,我使用了以下构造(第一次尝试服务器)。
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream is,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
Map<String, String> map = fileDetail.getParameters();
}
我可以保存文件,但我可以从“fileDetail”获得的唯一信息是文件名。调用“1st-attempt-server”的客户端(“1st-attempt-client”)如下所示:
public void uploadFile() throws IOException, URISyntaxException {
String endpoint = "http://localhost:8080/Uploadv03/rest/upload";
String filePath = "/tmp/testimage.jpg";
File file = new File(filePath);
FileBody fileBody = new FileBody(file,ContentType.DEFAULT_BINARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("file", fileBody);
String owner="me-myself";
builder.addTextBody("owner", owner, ContentType.TEXT_PLAIN);
HttpEntity entity = builder.build();
HttpPost request = new HttpPost(endpoint);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
try {
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statuscode "+statusCode);
} catch (IOException e) {
e.printStackTrace();
}
}
查看服务器中的地图,该地图只给了我两个密钥对,它们是(名称=文件和文件名=testimage.jpg)-似乎我无法获取“所有者”属性。
在我的第二次尝试中,我使用了以下构造(第二次尝试服务器)。我确信我在几年前就使用过这个结构。
@POST
@Path("/load")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response external(@MultipartForm FileUploadForm form) throws IOException {
logger.info("the 'load'-endpoint ");
}
支持文件:FileuploadForm 看起来像这样(带有 getter/setter)
public class FileUploadForm implements Serializable {
private static final long serialVersionUID = 1L;
public FileUploadForm() {
}
@FormParam("selectedFile")
@PartType("application/octet-stream")
private byte[] file;
@FormParam("owner")
private String owner;
getters/setters
}
调用“2nd-attempt-server”的客户端(“2nd-attempt-client”)如下所示:
private void post() throws FileNotFoundException, IOException {
System.out.println("PostClient2\n");
String endpoint = "http://localhost:8080/Uploadv03/rest/upload";
String owner = "me-myself";
String filename = "naming-the-file";
String filePath = "/tmp/testimage.jpg"
File file = new File(filePath);
FileInputStream inputStream = new FileInputStream(file);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost uploadFile = new HttpPost(URL);
MultipartEntityBuilder builder= MultipartEntityBuilder.create();
builder.addTextBody("owner", owner, ContentType.TEXT_PLAIN);
builder.addBinaryBody("file", inputStream);
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = null;
try {
response = httpClient.execute(uploadFile);
} catch (IOException ex) {
System.err.println("Unable to make connection");
}
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Statuscode "+statusCode);
}
现在我收到HTTPS 状态 415作为响应。
第二台服务器,客户端代码缺少什么?如果我改成这个似乎并不重要
builder.addBinaryBody("selectedFile", inputStream);
我确实喜欢这里使用 POJO 的第二种方法,我正在 Tomcat 8 中部署代码。当然,这里的球衣代码就足够了,我也看到了一些其他依赖项,但我希望这个“干净”的例子能够工作如果可行(我在指南中找不到简单的示例)
对于服务器端代码,我正在使用这些球衣依赖项(我的 pom.xml 的一部分),并且我正在使用 Tomcat 8 中的代码。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>1.19.4</jersey.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<!-- Works for Tomcat 8-->
<dependencies>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-servlet -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-json -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-multipart-provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>4.3.1.Final</version>
</dependency>