Java, multipart: determine whether the uploaded multipart is an image

I'm developing a spring MVC application. I have a file upload tool To this end, I want to determine whether the file uploaded by the user is an image

I want to create a thumbnail preview of the image, which is why I need to determine whether it is an image I have thumbnail creation code, nothing can know whether it is an image

Request code:

@RequestMapping(value = "/notes/addattachment/{noteid}/{groupaccountid}/{api}",method = RequestMethod.POST)
    public @ResponseBody void addAttachments(@PathVariable("noteid") int noteid,@PathVariable("groupaccountid") Long groupAccountId,@PathVariable("api") String api,@RequestParam("attachments") multipartfile attachment) {
        if (!(attachment.isEmpty())) {
            switch (api){
                case "somecase":
                    try {

                        String fileName = attachment.getOriginalFilename();
                        long fileSize = attachment.getSize();
                        byte[] bytes = attachment.getBytes();
                        this.groupAttachmentsService.addAttachment(bytes,fileName,fileSize,noteid,true,attachment.getContentType());
                        } catch (Exception ignored) {}
                    break;
                case "google":
                    this.driveQuickstart.insertFile(attachment,groupAccountId,"123");
                    break;
                case "drop@R_681_2419@":
                    String path = api.replace(":","/");
                    this.drop@R_681_2419@Task.insertFile(attachment,"path",groupAccountId);
                    break;


            }
        }
    }

I'd be glad if you could help thank you.

Solution

If you are using java 7, you can use imageio to check whether the file is an image Imageio read() this method returns NULL if no image is found

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>