attachment limit

Hi ,

Can we set a limit for attachment size


Regards,
Niyas

I guess what you mean is uploaded file size limit.

There is no direct API for that, but you can create your own Receiver for the upload with a custom OutputStream that limits the file size. Something like (for receiving in an in-memory buffer):

    public OutputStream receiveUpload(String filename, String MIMEType) {
        fileName = filename;
        mtype = MIMEType;
        // output stream that limits the maximum file size - see
        // ByteArrayOutputStream for more details
        stream = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                int newcount = count + 1;
                if (maxFileSize > 0 && newcount > maxFileSize) {
                    throw new IOException("Maximum allowed file size ("
                            + maxFileSize + ") exceeded");
                }
                if (newcount > buf.length) {
                    byte[] newbuf = new byte[buf.length << 1]
;
                    System.arraycopy(buf, 0, newbuf, 0, count);
                    buf = newbuf;
                }
                buf[count]
 = (byte) b;
                count = newcount;
            }

            @Override
            public synchronized void write(byte b[], int off, int len)
                    throws IOException {
                if ((off < 0) || (off > b.length) || (len < 0)
                        || ((off + len) > b.length) || ((off + len) < 0)) {
                    throw new IndexOutOfBoundsException();
                } else if (len == 0) {
                    return;
                }
                int newcount = count + len;
                if (maxFileSize > 0 && newcount > maxFileSize) {
                    throw new IOException("Maximum allowed file size ("
                            + maxFileSize + ") exceeded");
                }
                if (newcount > buf.length) {
                    byte[] newbuf = new byte[Math.max(buf.length << 1,
                            newcount)];
                    System.arraycopy(buf, 0, newbuf, 0, count);
                    buf = newbuf;
                }
                System.arraycopy(b, off, buf, count, len);
                count = newcount;
            }
        };
        return stream;
    }

I’m not sure if overriding both variants of write() is necessary.