java批量读取本地指定文件夹的所有图片

批量读取本地图片转化为流

 		File file = new File("E://testPics");
        File[] files = file.listFiles();
        for (File f : files) {
            try {
                //读取图片转换为流
                FileInputStream fis = new FileInputStream(f);
              	//(ps:此工具方法贴在了下方)
             	 byte[] data = IOUtil.inputStreamToBytes(fis);
                String name = f.getName();
                String fileType = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();
                //上传至图片服务器(ps:此处可忽略,此文此方法是将图片上传至相应图片服务器后返回的图片url地址)
                String picUrl = uploadFile(data, fileType);
                //输出流写到本地
                writeUrls(picUrl,"E://urls.txt");
            } catch ( Exception e) {
                logger.warn("读取上传异常",e);
                continue;
            }

        }

批量将获取的文件url写出到本地txt文件

public void writeUrls(String url,String outPath) throws IOException {
    File txt = new File(outPath);
    url = url + "\r\n";
    byte[] bytes = new byte[512];
    bytes = url.getBytes();
    int length = bytes.length;

    FileOutputStream fos = new FileOutputStream(txt,true);
	fos.write(bytes, 0, length);
    fos.flush();
    fos.close();
}

字节流转化位bytes的方法


    public static byte[] inputStreamToBytes(InputStream ins){
    	byte[] data = null;
    	ByteArrayOutputStream baos = null;
    	try
		{
            baos = new ByteArrayOutputStream();
            int i = -1;
            byte[] buf = new byte[BUFFER_SIZE];
            while ((i = ins.read(buf)) != -1) 
            {
                baos.write(buf, 0, i);
            }
            data = baos.toByteArray();
		}
		catch (IOException e)
		{
			//TODO: 错误处理
			
		}

        return data;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值