1、数据库存储二进制流使用的数据类型为BLOB

2、mybatis语句

  • 二进制流存入数据库
	<update id="saveKeyValue" parameterType="java.util.Map" statementType="PREPARED">
		update license set keyValue=#{content,jdbcType=BLOB} where id=#{id}
	</update>

需要注意的是keyValue=#{content,jdbcType=BLOB},此处传参使用的是Map,mybatis会自动获取指定的key。

  • 读取数据库中的二进制流。
    <select id="getKeyValue" parameterType="java.lang.String"    resultType="java.util.Map" statementType="PREPARED" >
     	select keyValue from license where id=#{id}
    </select>

这个没有去纠结具体的返回类型是什么,直接使用map进行接收。

3、mapper不做任何处理,直接传参

    /**
     * 保存key值
     * @param map
     */
    public void saveKeyValue(Map<String, Object> map);
    
    /**
     * 获取key值
     * @param id
     * @return
     */
    public Map<String, Object> getKeyValue(String id);

4、service需要进行逻辑处理

  • 存储二进制流
	/**
	 * 通过file获取key并保存
	 * @param source
	 */
	public void readKeyValue(File source,String id) {
		InputStream input = null;
		byte[] content = null;
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("id", id);
		try {
	           input = new FileInputStream(source);       
	           byte[] buf = new byte[1024];        
	           int len;        
	           while ((len = input.read(buf)) != -1) {
	        	   java.sql.Blob blob=null;
	        	   content = Arrays.copyOfRange(buf, 0, len);
	        	   blob = new SerialBlob(content);
	        	   map.put("content", blob);
	           }
	    }catch(Exception e){
	    	e.printStackTrace();
	    }finally {
	        try {
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		licenseMapper.saveKeyValue(map);
	}

大致过程:

  1. 获取文件的位置
  2. 获取文件的二进制流

      特别需要注意的就是这段代码。获取二进制流之后,需要对流进行操作。转成blob文件

java.sql.Blob blob=null;
content = Arrays.copyOfRange(buf, 0, len);
blob = new SerialBlob(content);
map.put("content", blob);

      3.调用mybatis存储数据

  • 读取二进制流
    	/**
    	 * 通过id获取keyvalue并写入key.txt
    	 * @param dest
    	 * @param id
    	 */
    	public void writeKeyValue(File dest,String id) {
    		OutputStream output = null;
    	    try {
    	           output = new FileOutputStream(dest); 
    	           Map<String, Object> map = licenseMapper.getKeyValue(id);
    	           output.write((byte[]) map.get("keyValue"));
    
    	    } catch(Exception e) {
    	    	e.printStackTrace();
    	    } finally {
    	        try {
    				output.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	    }
    	}

     

大致过程:

  1. 首先通过mybatis获取对象实体,
  2. 通过实体获取二进制流。

这个地方需要注意:其实取出的二进制需要实质是byte[]

     3.输出流将文件输出到硬盘上。

最后跟前端页面的交互记录一下

前端页面传文件时使用的是

enctype="multipart/form-data"

web层可以直接使用下面的代码。获取文件的二进制流。

	@RequestMapping(value = "/license/create",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject createLicense(HttpSession session,HttpServletRequest request,Model model,@RequestParam("file") MultipartFile file) throws Exception{
			byte[] content = file.getBytes();
}

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐