简体   繁体   中英

Upload image as byte[] via Spring Rest

I am trying to upload an image file as a byte[] object. At the web service, convert this byte[] into BufferedImage and perform some processing before returning the byte[] as a response to the client.

But I am always getting null pointer when ImageIO.read(byte[] input) is called. I noticed that the byte[] length before and after invoking the service is different. Please help. Following the codes for the client and the rest web service.

client code: CloseableHttpClient httpclient = null;

    final String uri = "http://localhost:8080/core/uploadForMarkImg";

    File source_imgFile = new File("C:\\watermarktest\\source.jpg");


    try
    {
    ImageInputStream iis = ImageIO.createImageInputStream(source_imgFile);

    Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);

    while (imageReaders.hasNext()) {
        ImageReader reader = (ImageReader) imageReaders.next();
        System.out.printf("formatName: %s%n", reader.getFormatName());
    }
    }catch (Exception ex)
    {
        ex.printStackTrace();
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try
    {

    BufferedImage img=ImageIO.read(source_imgFile);
    ImageIO.write(img, "jpg", bos);
    bos.flush();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

httpclient = HttpClients.custom().build();
    HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody(
            "imgFile", bos.toByteArray(), ContentType.APPLICATION_OCTET_STREAM, source_imgFile.getName()).build();

    post.setEntity(entity);

Server Code:

@RequestMapping(value = "/uploadForMarkImg", method = RequestMethod.POST, consumes = { "application/octet-stream" })
public ResponseEntity<byte[]> imgMark(@RequestBody byte[] imgFile) {


    try {
        ClassLoader classLoader = getClass().getClassLoader();
        File watermarkImageFile = new File(classLoader.getResource("watermark.png").getFile());


        System.out.println("input file length " + imgFile.length);
        BufferedImage resizeOriginal = null;
        InputStream inputStream = new ByteArrayInputStream(imgFile);
        BufferedImage sourceImage=ImageIO.read(new ByteArrayInputStream(imgFile));
System.out.println("converted to buffer image completed for source image " + sourceImage);

        BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

        System.out.println("converted to buffer image completed for watermark image " + watermarkImageFile);

        System.out.println("SouceImage Size Width: " + sourceImage.getWidth());
        System.out.println("SouceImage Size Height: " + sourceImage.getHeight());

sourceImage.get will always get nullpointer errror.

As per the code, you are trying to write the images bytes under imgFile multipart. Hence, when you want to retrieve those bytes, it should be via that part itself.

Try this:

@RequestMapping(value = "/uploadForMarkImg", method = RequestMethod.POST, consumes = { "application/octet-stream" })
public ResponseEntity<byte[]> imgMark(@RequestParam(value="imgFile") MultipartFile[] files) {

First element would give you the file. Or may be array is not required at all. Try this out.

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