简体   繁体   中英

png file Uploading Restful Web Service java

I try to write an image file uploading post method for my web service. Here is my post method. The image file can be uploaded into post method but can not converted into Image type.

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/html")
public Response uploadFile(@FormDataParam("file") File file2) throws IOException {
    InputStream IS = null;

    String output = "";

    try {
        IS = new FileInputStream(file2);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

    }


    try {
        if (file2 != null && IS != null) {
            Image img = ImageIO.read(IS);
            if (img != null) {
                output = "image file transmission sucessed";

            } else {
                String out = convertStreamToString(IS);
                output = "file uploaded into post method, however can not transfer it into image type "+
                "\n"+ out;
            }

        } else if (file2 == null) {
            output = "the file uploaded into post method is null";
        }

    } catch (IOException e) {
        e.printStackTrace();

    }

    return Response.status(200).entity(output).build();

}

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

The "file uploaded into post method, however can not transfer it into image type "+"\\n"+ out; message is shown. The reason I believe is the inputStream contants extral file information + the content of the image. When I try to convert the inputStream back to Image, I need to find a way to get rid of the extra info passed.

here is my new reversion:

@POST
@Path("/images")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response imageUpload(
        @FormDataParam("image") InputStream hereIsImage,
        @FormDataParam("image") FormDataContentDisposition hereIsName) {
    String path = "f://";
    if (hereIsName.getSize() == 0) {
        return Response.status(500).entity("image parameter is missing")
                .build();
    }
    String name = hereIsName.getFileName();
    path += name;

    try {
        OutputStream out = new FileOutputStream(new File(path));
        int read;
        byte[] bytes = new byte[1024];
        while ((read = hereIsImage.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        return Response.status(500)
                .entity(name + " was not uploaded\n" + e.getMessage())
                .build();
    }
    return Response.status(200).entity(name + " was uploaded").build();
}

However, when I upload the image, an error pops up:

[ERROR] An exception occurred during processing of the au.edu.rmit.srtwebservice.util.rest.testWS class. This class is ignored. com/sun/jersey/core/header/FormDataContentDisposition

the testWS.calss is where my method is.

public Response uploadFile(@FormDataParam("file") File file2)

File input may not work because you most likely be sending an octet stream from your client. So try using InputStream as the input type and hopefully it should work.

public Response uploadFile(@FormDataParam("file") InputStream file2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM