我有一个休息的webservice,它接受带有多部分消息的POST metod:
@Path("transferFile")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_XML)
public String multipartTest(com.sun.jersey.multipart.MultiPart data) {
try {
// get first body part (index 0)
BodyPart bp = multiPart.getBodyParts().get(0);
etc..
现在我正在尝试为此编写一个Java客户端。我从一个简单的球衣客户开始: 查看plaincopy到clipboardprint?
MultiPart multiPart = new MultiPart();
multiPart.bodyPart( new BodyPart(wavestream,MediaType.APPLICATION_OCTET_STREAM_TYPE));
Client c = Client.create();
WebResource r = c.resource("http://127.0.0.1:8080/webapp:);
response=r.path("transferFile").type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_XML).post(String.class, multiPart);
这很有效 - 一切都很好。但是我需要这个客户端在Android上工作,我在该平台上使用泽西时遇到了麻烦。所以我用普通方式在android上发送multipart消息:
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second
HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/webapp/transferFile");
httpPost.setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA );
//tried with and without base64
byte [] encodedWavestream = Base64.encodeBytesToBytes(wavestream);
InputStream ins = new ByteArrayInputStream(encodedWavestream);
InputStreamBody body = new InputStreamBody(ins, "test" );
int send = ins.available();
MultipartEntity requestContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE );
requestContent.addPart("stream", body);
httpPost.setEntity(requestContent);
HttpResponse Response = client.execute(httpPost);
这会给服务器带来恼人的响应:
HTTP Status 400 - Bad Request
The request sent by the client was syntactically incorrect (Bad Request).
我检查服务器日志文件但没有任何内容。所以我不知道这个错误的起源是什么。我写了一个简单的html页面,其中包含一个post公式和'multipart / form-data'内容类型,它也有效! soapUI的自动生成请求也有效!为什么我的客户不起作用?有人可以帮忙吗?
答案 0 :(得分:1)
泽西岛有错误。请参阅Chunked encoding problem。
此问题仅适用于少数客户端(iOS,Android)。
如果将Content-Type设置为application / octet-stream,那么application / octet-stream的Jersey MessageWriter将设置Content-Length和 不发送为分块传输方法。
Jersey客户端有solution:
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE, 32 * 1024);
但它不适用于 iOS或Android的客户端。 所以我测试了Apache文件上传。 Threre是另一个错误:“Stream意外结束了。”
只有 Oreilly upload 可以为所有客户端上传文件。 这是我的代码:
public Object[] getParametersAndFiles(HttpServletRequest request) throws IOException {
log.debug("OreillyUpload");
Properties params = new Properties();
LinkedHashMap files = new LinkedHashMap();
File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
params.put(name, value);
log.debug("param; name=" + name + ", value=" + value);
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
File file = new File(tempDirectory,fileName);
long size = filePart.writeTo(file);
files.put(name, file);
log.debug("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePart.getFilePath() +
", content type=" + filePart.getContentType() +
", size=" + size);
}
else {
// the field did not contain a file
log.debug("file; name=" + name + "; EMPTY");
}
}
}
return new Object[] {params, files};
}
这是泽西岛服务器代码(警告所有泽西岛上传的索引(如“@FormDataParam”)应该删除):
@POST
@Path("uploadMarkup")
@Produces(MediaType.APPLICATION_JSON)
// @Consumes(MediaType.MULTIPART_FORM_DATA)
//// public void uploadMarkup(
// public JSONWithPadding uploadMarkup(
// @FormDataParam("markupFile") InputStream markupFile,
// @FormDataParam("markupFile") FormDataContentDisposition details,
// @FormDataParam("slideNum") int slideNum) {
public JSONWithPadding uploadMarkup(@Context HttpServletRequest request) {
Object[] data = uploadService.getParametersAndFiles(request);
...
}