Link to home
Start Free TrialLog in
Avatar of blue-genie
blue-genieFlag for South Africa

asked on

DiskFile - empty file

I have a web based application that uses DiskFile to upload a file from the harddrive, stores it in a specific location and adds it to a database, however, if the file is empty, ie. has valid name and extension but nothing in it (such as a blank txt file) - it doesn't work, not throwing any errors, seems to just hang?
what's up with that?

can anyone explain, is this a known issue, and how should i catch it?
thanks.
blu.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you post your reading code please?
Avatar of blue-genie

ASKER

i'm thinking it's something to do with this bit but my java knowledge ain't good enough to be sure.

        fileToWrite.createNewFile();
                FileOutputStream fos = new FileOutputStream(fileToWrite);
                FileChannel fco = fos.getChannel();
                int sz = (int)fc.size();
                //int bufferSize = 16384;
                ByteBuffer bb = ByteBuffer.allocate (sz);
                // make our file and output stream
                int nbytes = -1;
                int totBytes = 0;
                while ( (nbytes=fc.read(bb) )!= -1)
                {
                      logger.debug("Bytes written: " + bb);
                      bb.rewind();
                      int writBytes = fco.write(bb);
                      fco.force(false);
                      bb.clear();
                }
>>int sz = (int)fc.size();

Make sure sz is > 0 before proceeding
how should i handle it? let them load an empty file? or notify them and stop it?
check what line it is hanging on.
what exactly does the filechannel do?
>>let them load an empty file?

You may as well - i.e. let them *think* they're doing something, but do nothing. What's the point otherwise?
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, this is rather odd,
i've changed it as such...

if (sz == 0){
                      request.setAttribute("empty", "y");
                } else {
                //int bufferSize = 16384;
                ByteBuffer bb = ByteBuffer.allocate (sz);
                // make our file and output stream
                int nbytes = -1;
                int totBytes = 0;
                while ( (nbytes=fc.read(bb) )!= -1)
                {
                      logger.debug("Bytes written: " + bb);
                      bb.rewind();
                      int writBytes = fco.write(bb);
                      fco.force(false);
                      bb.clear();
                }
                }

but now, it seems to be working, uploads the file, but the logic present is not happening - does this mean it aint' zero
>>but the logic present is not happening

What do you mean by that? Are you getting logging messages?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah that always freaks me out when i don't do anything and the code starts working, will get back to u'all.
Maybe you were using an older version of your class-file and it just needed a recompilation/ re-deployment ;-)