EAS BOS 图片的上传和下载(Java JDBC版)

一、背景:

      1.1):前台界面将图片存储到数据

      1.2):前台界面根据数据库存储的图片信息进行显示处理

 

二、实现方法:

JDBC 方式实现图片代码的存储读取。
注意:sql server 图片字段的类型定义为 IMAGE

 

三:具体事项

      3.1):图片存储表 test_Img

CREATE TABLE test_Img
  (
     CFCarNumber [VARCHAR](44) NOT NULL,
     CFCarImg    IMAGE
  ) 

数据库存储的样例:

     3.2):用户前台界面

 

    3.3):相关代码

服务端代码:

   /**
     * 图片插入
     */
    @Override
    public void saveImg(Context ctx) throws BOSException {
    	super.saveImg(ctx);
    	Connection conn = null;
    	
    	try {
    		conn = DBUtils.getConnection(ctx);
    		String sql = "insert into test_Img (CFCarNumber,CFCarImg) values (?,?) ";
    		PreparedStatement pstmt=conn.prepareStatement(sql); 
    		FileInputStream fi=new FileInputStream("D:\\temp\\马斯顿丁1.jpg"); 
    		pstmt.setString(1, "trt_003");
    		pstmt.setBinaryStream(2,fi,fi.available()); 
    		pstmt.execute(); 
    	} catch (SQLException e) {
    		e.printStackTrace();
    	} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    		
    	
    }
    
    
    /**
     * 图片读取
     */
    @Override
    public Icon readImg(Context ctx, String code) throws BOSException,
    		EASBizException {
    	
    	ImageIcon icon = null;
    	Connection conn = null;
    	
    	try {
			conn = DBUtils.getConnection(ctx);
			PreparedStatement ps = conn.prepareStatement("select CFCarImg from test_Img where CFCarNumber='"+code+"' ");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
            	 InputStream in = rs.getBinaryStream("CFCarImg");
            	 FileOutputStream out = new FileOutputStream("D:\\temp\\new.jpg");
                 byte[] b = new byte[1024];
                 int len = 0;
                 while ((len = in.read(b)) != -1) {
                     out.write(b, 0, len);
                     out.flush();
                 }
            }

            Image img = ImageIO.read(new File("D:\\temp\\new.jpg"));
            icon=new ImageIcon(img); 
            return icon;
            
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	return icon;
    	
    }

 

客户端代码:

    @Override
    public void onLoad() throws Exception {
    	super.onLoad();
    	readImg();
    }
    
    /**   
	 * @Title: readImg   
	 * @Description: TODO(读取图片)   
	 * @param:       
	 * @return: void      
	 * @throws   
	 */
	private void readImg() {
		
		KDLable.setBounds(new Rectangle(520, 75, 526, 361));
		kDLabel1.setBounds(new Rectangle(614, 420, 100, 19));
		
		try {
			Icon icon = CarInfrormactionFactory.getRemoteInstance().readImg(txtNumber.getText());
			KDLable.setIcon(icon);
			KDLable.setText(null);
			kDLabel1.setText("第1页/共5页");
			
		} catch (EASBizException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		}
		
	}

	
    @Override
    public void actionSubmit_actionPerformed(ActionEvent e) throws Exception {
    	super.actionSubmit_actionPerformed(e);
    	CarInfrormactionFactory.getRemoteInstance().saveImg();
    }

 

 


注意:

此贴代码为Demo,很多代码都是直接写死的,仅供参考,具体情况根据实际业务考虑。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值