blue-genie
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.
what's up with that?
can anyone explain, is this a known issue, and how should i catch it?
thanks.
blu.
Can you post your reading code please?
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(fileToWri te);
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();
}
fileToWrite.createNewFile(
FileOutputStream fos = new FileOutputStream(fileToWri
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
Make sure sz is > 0 before proceeding
ASKER
how should i handle it? let them load an empty file? or notify them and stop it?
check what line it is hanging on.
ASKER
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?
You may as well - i.e. let them *think* they're doing something, but do nothing. What's the point otherwise?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, this is rather odd,
i've changed it as such...
if (sz == 0){
request.setAttribute("empt y", "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
i've changed it as such...
if (sz == 0){
request.setAttribute("empt
} 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?
What do you mean by that? Are you getting logging messages?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ;-)