拒绝服务攻击是一种滥用资源性的攻击。从程序源代码角度讲,对涉及到系统资源的外部数据应该进行严格校验,防止无限制的输入。

    1. static final int MAX= 0x3200000; // 50MB
    2. // ...
    3. // write the files to the disk, but only if file is not insanely big
    4. if (file.length > MAX) {
    5. throw new IllegalStateException("File is huge.");
    6. }
    7. FileOutputStream fos = new FileOutputStream(file_name);
    8. bop = new BufferedOutputStream(fos, SIZE);
    9. while ((count = fileInputStream.read(bytes)) != -1) {
    10. bop.write(bytes, 0, count);
    11. }