Search code examples
javametadatajcifs

how to update the file in windows fileshare from the local machine using login credentials in java?


can anybody explain to me, how to proceed in the following scenario ?

i need to update the metadata(like :tag,title) of a file(like: docx,pptx etc) in windows fileshare from the local machine using login credentials in java .

note : 1.i have updated the metadata of files which is in local file system using Apache poi .

2.to access a file in the windows share i used jCIFS and i passed the smbFile object reference as a InputStream to POIFSFileSystem i am getting a error as below

java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes

this is the code i have tried :

public static void main(String[] args)
    {    

    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain","username","passw0rd$");

        SmbFile sFile = new SmbFile("smb://host/SharedFiles/adoc.doc", auth);

        sFile.connect();

        /* Open the POI filesystem. */
        InputStream is = new SmbFileInputStream(sFile);

        POIFSFileSystem poifs = new POIFSFileSystem(is);
        // is.close();


        /* Read the summary information. */
        DirectoryEntry dir = poifs.getRoot();
        SummaryInformation si;
        try
        {
            DocumentEntry siEntry = (DocumentEntry)
            dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(siEntry);
            PropertySet ps = new PropertySet(dis);
            dis.close();
            si = new SummaryInformation(ps);
        }catch (FileNotFoundException ex)
        {
            /* There is no summary information yet. We have to create a new
             * one. */
            si = PropertySetFactory.newSummaryInformation();
        }

        si.setKeywords("mykeyword");

        //   some code ..................



        /* Write the summary information and the document summary information
         * to the POI filesystem. */
        si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
        dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
        FileOutputStream out = new FileOutputStream(poiFilesystem);
        poifs.writeFilesystem(out);
        out.close();

        }

the same code works if i try to update the file in local machine but its not working if i try to update in host machine .

plz suggest me is there any other way to do this ? thanks in advance .............


Solution

  • To write to the fileshare use Jcifs.Try the following code

    Sample:

    SmbFile destFile = new SmbFile("smb://host/SharedFiles/adoc.doc", auth);
    SmbFileOutputStream sfos = new SmbFileOutputStream(destFile);
    poifs.writeFilesystem(sfos);
    sfos.close();