将文件转成clob添加到Oracle数据库中

2018-08-10 11:16:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Controller层:

@Controller
@RequestMapping(value = {"/Test/TestController"})
public class TestController extends BaseController implements ServletContextAware {
    private ServletContext servletContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

 /**写入数据的方法
     * 添加文件信息
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping(value = {"/testFileAdd.html"}, method = RequestMethod.POST)
    public ModelAndView toTestFileAdd(@RequestParam(value = "Files", required = false) MultipartFile file, HttpServletRequest request) {
        Map<Object, Object> param = new HashMap<Object, Object>();
        String name = file.getOriginalFilename();
        InputStream stream = null;
        String ioStr = "";
        try {
            stream = file.getInputStream();
            //将流转换成String
            byte[] in = new byte[stream.available()];
            stream.read(in);
            ioStr = HQCodec.hexEncode(in); //调用工具类
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != stream) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        param.put("FILE_NAME", name);
        param.put("FILES", ioStr);
        param.put("ID", 99);
        try {
            accessService.insert("TestFile.insertFile", param); //调用方法插入
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("sys/user/test");
    }

      //写出的方法

 //跳转到修改文件页面并将文件写到本地E盘中
    @RequestMapping("/toEditFiles.html")
    public ModelAndView toFiles(Integer id) {
        Map<Object, Object> param = new HashMap<Object, Object>();
        Map<String, Object> model = new HashMap<String, Object>();
        param.put("ID", id);
        try {
            Map<Object, Object> map = accessService.queryObject("TestFile.select", param);
            String file_name = (String) map.get("FILE_NAME");
            //写出clob 数据
           Clob o = (Clob) map.get("FILES");
            Reader stream = o.getCharacterStream();
            char[] c = new char[(int) o.length()];
            stream.read(c);
            String s = new String(c);
            stream.close();
            byte[] iobyte = HQCodec.hexDecode( s);
            File file = new File("E:"+file_name);
            OutputStream output = new FileOutputStream(file);
            output.write(iobyte);
            output.close();
            //将查询结果放入模型中
            model.put("TestFile", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("sys/user/toEditFiles", model);
    }

}

//转换类
public class HQCodec {
 
    static final char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };
 
    public static String hexEncode(byte[] buffer) {
        if (buffer.length == 0) {
            return "";
        }
        int holder = 0;
        char[] chars = new char[buffer.length * 2];
        for (int i = 0; i < buffer.length; i++) {
            holder = (buffer[i] & 0xf0) >> 4;
            chars[i * 2] = HEX[holder];
            holder = buffer[i] & 0x0f;
            chars[(i * 2) + 1] = HEX[holder];
        }
        return new String(chars);
    }
 
    public static byte[] hexDecode(String hex) {
        //A null string returns an empty array
        if (hex == null || hex.length() == 0) {
            return new byte[0];
        } else if (hex.length() < 3) {
            return new byte[] { (byte) (Integer.parseInt(hex, 16) & 0xff) };
        }
        //Adjust accordingly for odd-length strings
        int count = hex.length();
        int nibble = 0;
        if (count % 2 != 0) {
            count++;
            nibble = 1;
        }
        byte[] buf = new byte[count / 2];
        char c = 0;
        int holder = 0;
        int pos = 0;
        for (int i = 0; i < buf.length; i++) {
            for (int z = 0; z < 2 && pos < hex.length(); z++) {
                c = hex.charAt(pos++);
                if (c >= 'A' && c <= 'F') {
                    c -= 55;
                } else if (c >= '0' && c <= '9') {
                    c -= 48;
                } else if (c >= 'a' && c <= 'f') {
                    c -= 87;
                }
                if (nibble == 0) {
                    holder = c << 4;
                } else {
                    holder |= c;
                    buf[i] = (byte) holder;
                }
                nibble = 1 - nibble;
            }
        }
        return buf;
    }
}

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:elasticSearch学习安装

下一篇:python爬虫