Java 检查图片完整性代码

最近使用爬虫下载图片,遇到ctrl+C或者INT进行中断时,部分图片不能下载完整。因此需要筛选出这部分不完整图片的代码。

这里仅列出image/jpeg的例子:

import org.junit.Test;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.*;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;

/**
 * Description: To do
 * <p>
 * Author : Adore Chen
 * Created: 2018-08-22
 */
public class ImageCheck {

    @Test
    public void testAll(){
        int count = 0;
        File dir = new File("/tmp/data/pics/");
        for(File file: dir.listFiles()){
            if(!isJPEG(file.getAbsolutePath())){
                System.out.println(file.getName());
                //file.delete();
                count++;
            }
        }
        assertEquals(0, count);
    }

    private Boolean isJPEG(String fileName) {
        boolean canRead = false;
        try(ImageInputStream iis = ImageIO.createImageInputStream(new File(fileName))){
            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
            while (readers.hasNext()) {
                ImageReader reader = readers.next();
                reader.setInput(iis);
                reader.read(0);
                canRead = true;
                break;
            }
        }catch (Exception e){

        }

        return canRead;
    }

}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个较为完整的 Java 银行系统代码实现,实现了存款、取款、转账、查询余额、查询交易记录等功能。代码中使用了面向对象的编程思想和设计模式,同时对数据进行了封装和安全控制,提高了系统的可靠性和稳定性。 ``` import java.util.ArrayList; import java.util.Date; import java.util.Scanner; /** * 银行账户类 */ class Account { private String id; // 账户编号 private String name; // 账户名称 private String password; // 账户密码 private double balance; // 账户余额 private ArrayList<Transaction> transactions; // 交易记录 public Account(String id, String name, String password) { this.id = id; this.name = name; this.password = password; this.balance = 0; this.transactions = new ArrayList<>(); } public String getId() { return id; } public String getName() { return name; } // 存款 public void deposit(double amount) { balance += amount; transactions.add(new Transaction(new Date(), 'D', amount, balance)); } // 取款 public void withdraw(double amount) { if (balance < amount) { System.out.println("余额不足!"); } else { balance -= amount; transactions.add(new Transaction(new Date(), 'W', amount, balance)); } } // 转账 public void transfer(Account target, double amount) { if (balance < amount) { System.out.println("余额不足!"); } else { balance -= amount; target.balance += amount; transactions.add(new Transaction(new Date(), 'T', amount, balance, target.getId())); } } public double getBalance() { return balance; } // 查询交易记录 public void printTransactions() { for (Transaction transaction : transactions) { System.out.println(transaction.toString()); } } // 验证密码是否正确 public boolean validatePassword(String password) { return this.password.equals(password); } } /** * 交易记录类 */ class Transaction { private Date date; // 交易日期 private char type; // 交易类型(D:存款,W:取款,T:转账) private double amount; // 交易金额 private double balance; // 余额 private String targetId; // 目标账户编号(仅在转账时使用) public Transaction(Date date, char type, double amount, double balance) { this.date = date; this.type = type; this.amount = amount; this.balance = balance; } public Transaction(Date date, char type, double amount, double balance, String targetId) { this.date = date; this.type = type; this.amount = amount; this.balance = balance; this.targetId = targetId; } @Override public String toString() { return "Transaction{" + "date=" + date + ", type=" + type + ", amount=" + amount + ", balance=" + balance + (targetId != null ? ", targetId='" + targetId + '\'' : "") + '}'; } } /** * 银行系统类 */ public class BankSystem { private ArrayList<Account> accounts; // 所有账户 public BankSystem() { this.accounts = new ArrayList<>(); } // 创建账户 public void createAccount(String id, String name, String password) { accounts.add(new Account(id, name, password)); } // 登录账户 public Account loginAccount(String id, String password) { for (Account account : accounts) { if (account.getId().equals(id) && account.validatePassword(password)) { return account; } } return null; } public static void main(String[] args) { BankSystem bank = new BankSystem(); Scanner scanner = new Scanner(System.in); Account currentAccount = null; while (true) { if (currentAccount == null) { System.out.println("欢迎使用银行系统,请选择操作:"); System.out.println("1. 创建账户"); System.out.println("2. 登录账户"); System.out.println("3. 退出"); System.out.print("请选择操作编号:"); int option = scanner.nextInt(); if (option == 1) { System.out.print("请输入账户编号:"); String id = scanner.next(); System.out.print("请输入账户名称:"); String name = scanner.next(); System.out.print("请输入账户密码:"); String password = scanner.next(); bank.createAccount(id, name, password); System.out.println("账户创建成功!"); } else if (option == 2) { System.out.print("请输入账户编号:"); String id = scanner.next(); System.out.print("请输入账户密码:"); String password = scanner.next(); currentAccount = bank.loginAccount(id, password); if (currentAccount == null) { System.out.println("登录失败,请检查账户编号和密码!"); } else { System.out.println("登录成功,欢迎 " + currentAccount.getName() + "!"); } } else if (option == 3) { break; } else { System.out.println("输入有误,请重新输入!"); } } else { System.out.println("请选择操作:"); System.out.println("1. 存款"); System.out.println("2. 取款"); System.out.println("3. 转账"); System.out.println("4. 查询余额"); System.out.println("5. 查询交易记录"); System.out.println("6. 退出登录"); System.out.print("请选择操作编号:"); int option = scanner.nextInt(); if (option == 1) { System.out.print("请输入存款金额:"); double amount = scanner.nextDouble(); currentAccount.deposit(amount); System.out.println("存款成功!当前余额为:" + currentAccount.getBalance()); } else if (option == 2) { System.out.print("请输入取款金额:"); double amount = scanner.nextDouble(); currentAccount.withdraw(amount); System.out.println("取款成功!当前余额为:" + currentAccount.getBalance()); } else if (option == 3) { System.out.print("请输入转账金额:"); double amount = scanner.nextDouble(); System.out.print("请输入目标账户编号:"); String targetId = scanner.next(); Account targetAccount = null; for (Account account : bank.accounts) { if (account.getId().equals(targetId)) { targetAccount = account; break; } } if (targetAccount == null) { System.out.println("目标账户不存在!"); } else { currentAccount.transfer(targetAccount, amount); System.out.println("转账成功!当前余额为:" + currentAccount.getBalance()); } } else if (option == 4) { System.out.println("当前余额为:" + currentAccount.getBalance()); } else if (option == 5) { System.out.println("交易记录如下:"); currentAccount.printTransactions(); } else if (option == 6) { System.out.println("退出登录成功!"); currentAccount = null; } else { System.out.println("输入有误,请重新输入!"); } } } scanner.close(); } } ``` 这个完整的银行系统代码实现了基本的存款、取款、转账、查询余额和查询交易记录等功能,同时对数据进行了封装和安全控制,提高了系统的可靠性和稳定性。需要注意的是,这只是一个简单的示例,实际的银行系统需要更多的功能和安全措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值