0

我正在尝试使用 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();
}

请帮助我理解这段代码有什么问题。提前致谢....

4

2 回答 2

0
public void saveEnrollments(InputStream inputStream, String url){
    try {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        client.addFilter(new LoggingFilter());
        WebResource webResource = client.resource(url);
        FormDataMultiPart fdmp = new FormDataMultiPart();
            fdmp.bodyPart(new StreamDataBodyPart("enrollments", inputStream));
        ClientResponse response = webResource.type(
                "multipart/form-data").post(ClientResponse.class,
                        fdmp);
        String response= response.getEntity(String.class);

        System.out.println(response);
        }
    catch (Exception e) {

        e.printStackTrace();

    }

}

试试这个功能希望对你有帮助。

于 2013-05-09T06:46:36.243 回答
0

我认为您需要在客户端代码中使用 FormDataMultipart oject 来创建多部分/表单数据。然后给它添加附件。确保Web服务端的附件名称和参数名称相同:这是带有FormDataMultipart的示例客户端代码:

    FormDataMultiPart mMultiPart = null;
    byte fileContent[] = readFile(fileName); /// readfile is a method read file bytes
    if (mMultiPart == null) {
        mMultiPart = new FormDataMultiPart();
    }
    mMultiPart.field(attachmentName, fileContent,MediaType.APPLICATION_OCTET_STREAM_TYPE);

希望能帮助到你!

于 2013-05-09T05:58:31.917 回答