webtier@glassfish.java.net

Re: where to store uploaded files

From: <webtier_at_javadesktop.org>
Date: Thu, 28 Oct 2010 14:36:52 PDT

I 'gathered' this code from somwhere ages ago, apologies
to the original author, can't remember where from, but
thanks anyway.

[code]
public byte[] storeFile(File fileToLoad) {

        InputStream is = null;
        try {
            is = new FileInputStream(fileToLoad);
        } catch (FileNotFoundException ex) {
            SQLog.log(ex.getMessage());
        }
        long length = fileToLoad.length();
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        try {
            while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
        } catch (IOException ex) {
            SQLog.log(ex.getMessage());
        }
        return bytes;
    }
[/code]

Message was edited by: healeyb
[Message sent by forum member 'healeyb']

http://forums.java.net/jive/thread.jspa?messageID=486461