6 Replies Latest reply on Apr 10, 2012 2:01 PM by 802569

    FileUpload - file header

      Hoping someone here has experience with Apache Commons FileUpload library. I'm using it to upload a few different file types to the server. I'd like to ensure that only specific file types are saved on the server by checking the file header for each item. I essentially have the same code as the example on apache's website however the getHeaders() method always returns null and I'm not sure why. Any ideas?
      DiskFileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      try {
         List items = upload.parseRequest(request);
         Iterator iter = items.iterator();
         while(iter.hasNext()) {
           DiskFileItem item = (DiskFileItem) iter.next();
           FileItemHeaders headers = item.getHeaders();
        • 1. Re: FileUpload - file header
          If the getHeaders() method returns null, I expect that's because there are no headers. Checking the API documentation should confirm it -- I don't have it at hand right now but you should, so I will leave that for you to do.

          Were you expecting a header which would identify the type of the file being uploaded? What do you mean by "file type" anyway?
          • 2. Re: FileUpload - file header
            Yes I was hoping I could grab the file header to determine the file type (i.e. png, txt, exe). The content-type property seems to be based off the file extension, which I can easily change. I wanted something a little more concrete... but maybe the header is not as concrete as I think it is.
            • 3. Re: FileUpload - file header
              I think the best you can do is to get the file name and extract the extension from it. Yes, people can rename files so that a Windows executable masquerades as a GIF image, but I really wouldn't expect the file upload process to be examining the contents of the upload and deciding what it really is. You would have to do that yourself if you really wanted that information.
              • 4. Re: FileUpload - file header
                How would I accomplish that? Is the file header not a good way to determine that?

                • 5. Re: FileUpload - file header
                  I don't know what you mean by "the file header".

                  If you're referring to the "headers" returned by FileItem.getHeaders(), those are HTTP headers, aren't they? And they are whatever headers the client decided to put into the upload. I can't imagine clients which analyze the contents of the files they are uploading and insert a header to say "This file is a Windows executable" or anything like that. It seems to me you're reading the documentation in wish-fulfillment mode.
                  • 6. Re: FileUpload - file header
                    This is what I mean by file header http://en.wikipedia.org/wiki/File_format#File_header

                    Basically an alternate way of determining a file type other than the extension.