Upload de imagens em JAX-RS

0 respostas
T

Pessoal, estou tentando enviar como upload uma imagem para o meu recurso JAX-RS através do método HTTP POST. Porém quando eu leio o arquivo temporário gerado pelo JAX-RS com ImageIO.read, o retorno é null. Alguém poderia me ajudar?

@POST
    @Produces("text/plain")
    @Path("handler")
    public String put(File file) {
        try {
            byte[] bytes = new byte[(int) file.length()];
            InputStream is = new FileInputStream(file);

            is.read(bytes);

            for (int i=0; i<bytes.length; i++) {
                System.out.println(bytes[i]);
            }
            
            BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
            System.out.println("Imagem: " + img);
            ImageIO.write(img, "jpg", new File("/home/test/teste.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
            return "Upload error.";
        }

        return "Upload done.";
    }
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Form Page</title>
    </head>
    <body>
        <form enctype="multipart/form-data" action="http://localhost:8085/RestTest/res/content/handler" method="POST">
            <label for="song">Image:</label>
            <input type="file" name="image"/><br/>

            <input type="submit" value="OK"/>
            <input type="reset" value="Cancel"/>
        </form>
    </body>
</html>
Criado 19 de março de 2011
Respostas 0
Participantes 1