我想将所有本地电话联系人与我的服务器同步,所以我上传了所有与json有效负载服务器的联系人,所以我正在制作Multipart-form-data请求,但服务器每次都给出400错误
我的服务器代码是
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,MediaType.TEXT_PLAIN})
public Response handleUpload(@FormDataParam("filed1") InputStream stream) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while ((length = stream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
log.debug("string is "+result.toString("UTF-8"));
} catch (IOException e) {
// TODO Auto-generated catch block
log.debug("Exception "+e.getMessage());
}
// StandardCharsets.UTF_8.name() > JDK 7
return Response.ok().build();
}
我的MultiPart req类
public class MultiPartReq {
private static final String tag=MultiPartReq.class.getSimpleName();
private final String boundary;
private static final String LINE_FEED = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
public MultiPartReq(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
}
public void addFormField(String name, String value) {
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"")
.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.append("--").append(boundary);
writer.flush();
}
public void addHeaderField(String name, String value) {
writer.append(name + ":" + value).append(LINE_FEED);
writer.flush();
}
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
writer.append(LINE_FEED).flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
Log.e(tag,""+response.toString());
return response;
}
}
向我的服务器发送请求
private void postToUrl() {
String url="http://35.200.xx.xx:/example";
String charset="UTF-8";
try {
MultiPartReq multiPartReq=new MultiPartReq(url,charset);
multiPartReq.addHeaderField("Authorization","E9X422gPUg39wvsM");
multiPartReq.addHeaderField("Accept","application/json");
multiPartReq.addFormField("field1","test");
multiPartReq.finish();
}
服务器每次都给出400错误..当我尝试使用邮递员时它工作正常。我的代码出了什么问题我可以更改服务器和客户端代码。