0

I'm trying to find a way to get the Author of the Excel file that is uploaded. Once the Upload button is clicked, all I can see is the 'byte[] contents' and I'm confused to find a way to find the author of the uploaded file. I was getting the NoPropertySetStreamException when I run the below code.

public static Map<String, Boolean> validateFileSize(MultipartActionRequest request,
        List<String> appCaseId) {
    
    if (PortletFileUpload.isMultipartContent(request)) {
        Map<String, List<MultipartFile>> multipartFiles;

        multipartFiles = request.getMultiFileMap();

        if (multipartFiles != null && !multipartFiles.isEmpty()) {
            for ( List<MultipartFile> multipartFilesList : multipartFiles.values()) {
            for ( MultipartFile item : multipartFilesList) { 
                   byte[] contents;
                    try {
                        contents = item.getBytes();

                    try {
                        SummaryInformation si = (SummaryInformation)
                                 PropertySetFactory.create(item.getInputStream());
                        si.getLastAuthor();
                    } catch (NoPropertySetStreamException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (MarkUnsupportedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    } catch (IOException ioe) {
                        
                    }
Murali
  • 148
  • 1
  • 9
  • Here is an older but possibly quite helpful answer: https://stackoverflow.com/questions/12861054/how-to-get-file-summary-information-with-java-apache-poi – sorifiend Feb 10 '21 at 01:45
  • 1
    What kind of `Excel` file is uploaded? `PropertySetFactory` is for the old binary `BIFF`-format (`*.xls`) only. The current `Offiice Open XML`-Format (`*.xlsx`) has `POIXMLProperties`. – Axel Richter Feb 10 '21 at 06:26
  • @Alex - I'm trying to upload *.xlsx files – Murali Feb 10 '21 at 17:05

1 Answers1

1

There are two mainly different kinds of Microsoft Office files. The old binary OLE2 files (*.xls, *.doc, *.ppt, ...) and the newer Office Open XML (OOXML) files (*.xlsx, *.docx, *.pptx, ...).

For OLE2 the properties are in PropertySet which can be got via PropertySetFactory. But you need using it correctly. The InputStream used in PropertySetFactory.create(java.io.InputStream stream) is not meant to be the InputStream of the whole file. Instad it would must be a DocumentInputStream. Better approach would be using PropertySet.create(DirectoryEntry dir, java.lang.String name). See example below.

For OOXML the properties are in PackageProperties which can be got from the OPCPackage.

To determine whether a file is OLE2 or OOXML, FileMagic can be used. FileMagic.valueOf(java.io.InputStream inp) returns FileMagic.OLE2 or FileMagic.OOXML respectively.

The following example shows how to get the author out of the PropertySet of OLE2files and out of the PackageProperties of OOXML files. This shoud work for each kind of Microsoft Office which apache poi supports.

import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

import org.apache.poi.poifs.filesystem.FileMagic;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.SummaryInformation;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageProperties;

public class GetAuthor {

 static String getAuthor(InputStream is) throws Exception {

  String author = null;

System.out.println(FileMagic.valueOf(is));
  if (FileMagic.valueOf(is) == FileMagic.OLE2) {
   POIFSFileSystem fs = new POIFSFileSystem(is);
   PropertySet props = PropertySetFactory.create(fs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
   if (props instanceof SummaryInformation) {
    SummaryInformation sumInf = (SummaryInformation) props;
    author = sumInf.getAuthor();
   } 
  } else if(FileMagic.valueOf(is) == FileMagic.OOXML) {
   OPCPackage pckg = OPCPackage.open(is);
   PackageProperties props = pckg.getPackageProperties();
   author =  props.getCreatorProperty().get();
  }

  return author;

 }

 public static void main(String[] args) throws Exception {

  String author = null;

  InputStream is = new BufferedInputStream(new FileInputStream("./Excel.xls"));
  author = getAuthor(is);
System.out.println(author);
  is.close();

  is = new BufferedInputStream(new FileInputStream("./Excel.xlsx"));
  author = getAuthor(is);
System.out.println(author);
  is.close();

 }
}
Axel Richter
  • 56,077
  • 6
  • 60
  • 87