Tuesday, March 11, 2008

Upload File using a chunks of Bytes

If you want to upload a file using a fixed number of bytes in Struts than use following function.

public static void writeToDisk(FormFile file, String fileName,
String destinationPath) throws FileNotFoundException, IOException
{

InputStream stream = null;
OutputStream bos = null;

int fileSize = 0;
byte[] buffer = null;

if (log.isInfoEnabled()) log
.info("[writeToDisk] - Get InputFileStream and FileOutputStream");

stream = file.getInputStream();
bos = new FileOutputStream(destinationPath + fileName);

fileSize = file.getFileSize();

int sizeOfbytesToRead = 100;
// Total Bytes going to read from File.

buffer = new byte[sizeOfbytesToRead];
// Buffer which will contain the read bytes and
// work as storage.

if (log.isInfoEnabled())
log.info("[writeToDisk] - FileSize Found ("
+ fileSize + ") Buffer Length (" + buffer.length + ").");

int byteRead = 0;
int totalBytesRead = 0;
// If having less Data than Total Reading of bytes
if (sizeOfbytesToRead > fileSize)
{
byteRead = stream.read(buffer, 0, fileSize);
bos.write(buffer, 0, fileSize);
}
else
{
while (totalBytesRead < fileSize)
{
if (sizeOfbytesToRead < stream.available())
{
byteRead = stream.read(buffer, 0,
sizeOfbytesToRead);
bos.write(buffer, 0, sizeOfbytesToRead);
bos.flush();
}
else
{
int read = stream.available();
byteRead = stream.read(buffer, 0, read);
bos.write(buffer, 0, read);
bos.flush();
}

totalBytesRead += byteRead;
}
}

if (log.isInfoEnabled())
log.info("[writeToDisk] - Finished writing file(" + fileSize
+ ") to disk (" + destinationPath + ").");

bos.close();
stream.close();
}


======================================

Else if want to upload or copy a one file to another file using pure JAVA than use following function.

public static void writeToDisk(File file, String fileName,
String destinationPath) throws FileNotFoundException, IOException
{

InputStream stream = null;
OutputStream bos = null;

int fileSize = 0;
byte[] buffer = null;

if (log.isInfoEnabled()) log
.info("[writeToDisk] - Get InputFileStream and FileOutputStream");

stream = new FileInputStream(file);
bos = new FileOutputStream(destinationPath + fileName);

fileSize = (int)file.length();

int sizeOfbytesToRead = 100;

buffer = new byte[sizeOfbytesToRead];

if (log.isInfoEnabled())
log.info("[writeToDisk] - FileSize Found ("
+ fileSize + ") Buffer Length (" + buffer.length + ").");

int byteRead = 0;
int totalBytesRead = 0;
// If having less Data than Total Reading of bytes
try {
if (sizeOfbytesToRead > fileSize)
{

byteRead = stream.read(buffer, totalBytesRead, fileSize);
bos.write(buffer, totalBytesRead, fileSize);

}
else
{
while (totalBytesRead < fileSize)
{
if ((sizeOfbytesToRead) < stream.available())
{
log.info("------------------------");
log.info("TotalBytesRead :" +totalBytesRead + " | SizeOfbytesToRead: " + sizeOfbytesToRead);
log.info("Available : " +stream.available() + "..Buffer.." + buffer.length);

byteRead = stream.read(buffer, 0,
sizeOfbytesToRead);
//stream.mark(byteRead);
//stream.skip(sizeOfbytesToRead);
log.info(" BytesRead :" + byteRead);
log.info("------------------------");
//bos.flush();
bos.write(buffer, 0, sizeOfbytesToRead);
bos.flush();


}
else
{
log.info("*************************");
int read = stream.available();

byteRead = stream.read(buffer, 0, read);
//stream.
//stream.skip(sizeOfbytesToRead);
bos.write(buffer, 0, read);
log.info("*************************");
}

totalBytesRead += byteRead;
}
}

}
catch(IndexOutOfBoundsException e) {
log.info("Exception " + e.toString() );
//e.printStackTrace();
}

if (log.isInfoEnabled())
log.info("[writeToDisk] - Finished writing file(" + fileSize
+ ") to disk (" + destinationPath + ").");

bos.close();
stream.close();
}

1 comment:

Sriram said...

thanks it really helped me a lot :)

Contributors