0

I am using two jars commons-fileupload-1.2.2.jar and commons-io-2.0.1.jar to upload a file in my form. I am able to upload the file, but my requirement is to upload only .jpg/.jpeg file only. Below is my code. the problem is that if i change the extension of a pdf file to jpg file, it gets upload.

Currently i am checking it using content type of the file, the content type of changed pdf(into jpg) is also image/jpeg. I am not able to validate it. Please help

if (ServletFileUpload.isMultipartContent(request)) {
            try {

                List<FileItem> multiparts = new ServletFileUpload(
                        new DiskFileItemFactory()).parseRequest(request);

                for (FileItem item : multiparts) {

                    if (!item.isFormField()) {

                        filename = new File(item.getName()).getName();
                        content = item.getContentType();
                        System.out.println("content type: " + content);
                        System.out.println("name: " + filename);
                        sizeInBytes = item.getSize();


                        item.write(new File(UPLOAD_DIRECTORY + File.separator + filename));


                    }


                }

                if (!content.contains("jpeg")) {
                    System.out.println("Please upload jpeg file");

                } else {
                   // fupload = true;
                }



        } else {

            response.sendRedirect("upload.jsp");
        }
anonymous
  • 244
  • 2
  • 4
  • 23

1 Answers1

0

by the time you are doing it on the servlet it is too late, it is already uploaded. All you can do is check that the name ends with .jpg etc. Why not use <input type="file" accept="image/*">

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64