将文件存放到redis

import java.io.*;
原创@北京我辉哥
public class File2Redis {

        /*
        * 文件转数组
        * */
        public byte[] toByteArray(String path) throws IOException
        {
            if (path == null)
            {
                return null;
            }
            File f = new File(path);
            if (!f.exists())
            {
                return null;
            }
            BufferedInputStream in = null;//创建一个缓冲处理流
            try
            {
                in = new BufferedInputStream(new FileInputStream(f));
                byte[] buffer = new byte[10240];//数组大小应适当大于文件
                int len = 0;
                int i = 0;
                while (-1 != (len = in.read()))//逐个字节读取
                {
                    buffer[i] = (byte) len;//读取到的字节放进数组
                    i++;
                }
                byte[] buffer2 = new byte[i];//新建一个与文件大小相同的数组
                System.arraycopy(buffer, 0, buffer2, 0, i);//复制大数组中有效内容
                return buffer2;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }
            finally
            {
                try
                {
                    if (in != null)
                    {
                        in.close();
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        /*
        * 将文件转成数组存到redis
        * */
        public Boolean setFile(String key, String path)
        {
            if (key == null || key.equals("") || path == null || path.equals(""))
            {
                return false;
            }

            if (jedis != null)
            {
                try
                {
                    byte[] b = toByteArray(path);
                    if (b != null)
                    {
                        String json = jedis.set(key.getBytes(), b);
                        if (json != null && json.equals("OK"))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /*
        * redis中的数组读取出来转成输入流
        * */
        public static InputStream getInputStream(String key)
        {
            if (key == null || key.equals(""))
            {
                return null;
            }
            if (jedis != null)
            {
                try
                {
                    byte[] json = jedis.get(key.getBytes());//取出数组
                    if (json != null && json.length > 0)
                    {
                        try
                        {
                            InputStream inputStream = new ByteArrayInputStream(json);//转流
                            return inputStream;
                        }
                        catch (Exception e)
                        {
                            return null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    return null;
                }
            }
            else
            {
                return null;
            }
            return null;
        }


}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值