我正在尝试使用 Jersey Web Service 将文件从 android 上传到服务器。我已经编写了代码,但是代码中存在一些问题。它显示错误 - Java 类 com.sun.jersey.core.header.FormDataContentDisposition 和 Java 类型类 com.sun.jersey.core.header.FormDataContentDisposition 的消息正文阅读器,并且 MIME 媒体类型 multipart/form-data 不是发现 我已将 mimepull.jar 添加到类路径中,但仍然会生成此错误。
这是我的前端 Java 代码-
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
CONNECTION_TIMEOUT); // Timeout
HttpConnectionParams.setSoTimeout(httpParameters, SO_TIMEOUT);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
FileBody fileContent= new FileBody(file);
HttpResponse response;
HttpPost httppost = new HttpPost(Global.URL);
try {
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", fileContent);
httppost.setHeader("content-type", "multipart/form-data");
httppost.setEntity(entity);
response = client.execute(httppost);
HttpEntity resEntity = response.getEntity();
}catch(Exception e){
}
这是我的网络服务-
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/upload_file")
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
System.out.println("upload_file");
String uploadedFileLocation = "/aanbieding/assets/upload/requester/"
+ fileDetail.getFileName();
Util.saveToFile(uploadedInputStream, uploadedFileLocation); // save uploaded file to new location
String output = "File uploaded via Jersey based RESTFul Webservice to: "
+ uploadedFileLocation;
System.out.println(output);
return Response.status(200).entity(output).build();
}
请帮助我理解这段代码有什么问题。提前致谢....