Я пытаюсь загрузить файл, используя многочастный запрос, используя версию Java 1.6.Данные формы содержат два поля: 1. файл и 2. данные json.И в заголовке передается токен на предъявителя.Я написал код, как показано ниже.Пожалуйста, расскажите, где я делаю неправильно.Я пробовал несколько способов, но получал разные разные ошибки.
Ниже мой файл pom:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
<project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding>
<archetype.encoding>${encoding}</archetype.encoding>
<!-- Maven plugins version -->
<maven-archetype-plugin-version>2.2</maven-archetype-plugin-version>
<maven-resources-plugin-version>2.6</maven-resources-plugin-version>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.demo.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
</plugin>
</plugins>
</build>
Ошибки:
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/octet-stream, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
at com.cisco.client.TestFileUpload.App.exampleMultiPartUpload(App.java:64)
at com.cisco.client.TestFileUpload.App.main(App.java:19)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/octet-stream, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
... 6 more
код указан ниже.
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.MultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import org.json.JSONObject;
import javax.ws.rs.core.MediaType;
import java.io.*;
public class App {
public static void main(String[] args){
exampleMultiPartUpload( new File("<file_name>"));
}
public static void exampleMultiPartUpload(File fileToUpload) {
final String API_URI = "<URL>";
final ClientConfig config = new DefaultClientConfig();
final Client client = Client.create(config);
final WebResource resource = client.resource(API_URI);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
fileToUpload,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
fileDataBodyPart.setContentDisposition(
FormDataContentDisposition.name("file")
.fileName(fileToUpload.getName()).build());
final JSONObject jsonToSend = new JSONObject();
jsonToSend.put("fileName","test.txt");
jsonToSend.put("title","demo");
jsonToSend.put("description", "");
final MultiPart multiPart = new FormDataMultiPart()
.field("metadata", jsonToSend, MediaType.APPLICATION_JSON_TYPE)
.field("file", fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE)
.bodyPart(fileDataBodyPart);
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
ClientResponse response = resource
.header("Authorization", "Bearer <token>")
.post(ClientResponse.class,multiPart);
final String result = getStringFromInputStream(response.getEntityInputStream());
System.out.println("INFO >>> Response from API was: " + result);
client.destroy();
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
final StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}