users@jersey.java.net

[Jersey] Test Client for Multipart Jersy Rest Service

From: selvakumar netaji <vvekselva.gm_at_gmail.com>
Date: Thu, 23 Aug 2012 10:36:01 +0530

Hi,


I've created a new multipart part rest service and its working fine with
the html form. The rest service is

    @POST
    @Path("/Upload")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String uploadFile(@FormDataParam("file") InputStream inputStream,
            @FormDataParam("file") FormDataContentDisposition
contentDisposition) {

        System.out.println("Method Entry");
        System.out.println(contentDisposition.getFileName());


        String result = "not Success";
        File file = null;
        if (contentDisposition != null
                && contentDisposition.getFileName() != null
                && contentDisposition.getFileName().trim().length() > 0) {
            try {
                file = new File("xx"
                        + contentDisposition.getFileName());
                new File("xxx").mkdirs();
                file.createNewFile();
                OutputStream outputStream = new FileOutputStream(file);
                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
                outputStream.flush();
                outputStream.close();
                result = "success";

            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        System.out.println("Method Exit");
        return result;

    }

but the form the client code I'm not able to access the client


    FileInputStream finStream = new FileInputStream(new File(
                Error.txt"));

        FormDataMultiPart part = new FormDataMultiPart().field("file",
                finStream, MediaType.TEXT_HTML_TYPE);

        WebResource resource = Client.create().resource(
                "http://localhost:8080/Multipart/rest");
        String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE)
                .post(String.class, part);
        System.out.println(response);



Did I've made any mistakes.