You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by naciu <na...@gmail.com> on 2021/11/03 17:15:15 UTC

How upload a image file with rest cxf

Please some have a example or explain me how i can upload a file to server
with restful cxf implementation that came with tomee?

I founded this example:

https://bigdatanerd.wordpress.com/2012/03/11/rest-based-file-upload-using-apache-cxf-and-spring/

 @POST
    @Path("/file")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(List<Attachment> attachments,@Context
HttpServletRequest
request) {
        for(Attachment attr : attachments) {
            DataHandler handler = attr.getDataHandler();
            try {
                InputStream stream = handler.getInputStream();
                MultivaluedMap map = attr.getHeaders();
                OutputStream out = new FileOutputStream(new
File(getFileName(map)));

                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = stream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                stream.close();
                out.flush();
                out.close();
            } catch(Exception e) {
              e.printStackTrace();
            }
        }

        return Response.ok("file uploaded").build();
    }

    private String getFileName(MultivaluedMap<String, String> header) {

        String[] contentDisposition = header.getFirst("Content-Disposition"
).split(";");

        for (String filename : contentDisposition) {
            if ((filename.trim().startsWith("filename"))) {

                String[] name = filename.split("=");

                String finalFileName = name[1].trim().replaceAll("\"", "");
                return finalFileName;
            }
        }
        return "unknown";
    }
......      ............

But this line give me error:

MultivaluedMap map = attr.getHeaders();

............
On tomee8.08 the class

Attachment not have the letto e getHeaders();
La present only the letto e getHeadersNames();

Please some can contro il it?

The