我检查了很多相同问题的解决方案,但没有解决任何问题。这就是我再次发帖的原因。 当我使用POSTMAN时它正常工作。
我也试过没有编码。然后使用
“application / json; charset = utf-8” 我没有任何解决方案。
我的ClientCode:
public static void main(String[] args) throws JSONException, IOException
{
Test1 my_client = new Test1();
File file_upload = new File("C:/MyJSON.txt");
my_client.sendFileJSON(file_upload);
}
private void sendFileJSON(File file_upload) throws JSONException, IOException{
ClientConfig config = new DefaultClientConfig();
// config.getClass().add(MOXyJsonProvider.class);
Client client = Client.create(config);
client.addFilter(new LoggingFilter());
WebResource service = client.resource("https://hostedactivation.com/XXXXX/XXXXXXXX.php");
JSONObject data_file = new JSONObject();
data_file.put("file_name", file_upload.getName());
data_file.put("file", convertFileToString(file_upload));
ClientResponse client_response = service.type(MediaType.APPLICATION_JSON).header("Authorization", "Basic Y3lDi543XJzcEFsMkJ1Fm0xV2Ic5").post(ClientResponse.class, data_file);
System.out.println("Status: "+client_response.getEntity(String.class));
client.destroy();
}
//Convert my file to a Base64 String
private String convertFileToString(File file) throws IOException{
byte[] bytes = Files.readAllBytes(file.toPath());
return new String(Base64.encode(bytes));
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DataAnalytics</groupId>
<artifactId>DataAnalytics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm-all</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<!-- <dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.2</version>
</dependency> -->
</dependencies>
</project>
MyJSON.txt
{
"header":{
"user":"XXXX",
"password": "XXXXXXXXXXXXXX",
"table":"licf",
"method_override":"get"
}
}
我不知道,我错过了什么。欢迎提出任何意见。谢谢。
答案 0 :(得分:0)
Rcpp