日期:2014-05-16  浏览次数:20585 次

spring JdbcTemplate文件写数据库,读取
/**
	 * 插入文件表数据
	 * @param tb_File
	 * @return
	 */
	public int fileAdd(final File file,final Tb_File tb_File){
		// TODO Auto-generated method stub
		String strMethod = "fileAdd";
		logger.debug(strMethod + "Start.");
		//插入文件表数据
		StringBuffer INSERT_FILE_ADD=new StringBuffer();
		INSERT_FILE_ADD.append(" INSERT INTO TB_FILE(ID,IMAGE,IMAGESUFFIX,NAME)VALUES(?,?,?,?) ");
		int reFlag=2;//sql返回标记
		try {
			final InputStream is = new FileInputStream(file);
			final LobHandler lobHandler=new DefaultLobHandler(); 
			this.getJdbcTemplate().execute(new SQLProxy(INSERT_FILE_ADD.toString()).sql(),  
					                                 new AbstractLobCreatingPreparedStatementCallback(lobHandler){   
															protected void setValues(
																	PreparedStatement pstmt,
																	LobCreator lobCreator)
																	throws SQLException,
																	DataAccessException {
																// TODO Auto-generated method stub
																 pstmt.setString(1, tb_File.getId());
																 lobCreator.setBlobAsBinaryStream(pstmt,2,is,(int)file.length());  
																 pstmt.setString(3, tb_File.getImagesuffix());
																 pstmt.setString(4, tb_File.getName());
																
															}  
			                                     });  
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		reFlag=1;

		logger.debug(strMethod + "End.");
		return reFlag;
	}

/**
	 * 获取文件列表
	 * @return
	 */
	public List<Tb_File> getTbFile() {
		// TODO Auto-generated method stub
		String strMethod = "getTbFile";
		logger.debug(strMethod + "Start.");
		
		StringBuffer QUERY_GET_TB_FILE=new StringBuffer();
		QUERY_GET_TB_FILE.append(" SELECT ID,IMAGE,IMAGESUFFIX,NAME FROM TB_FILE ");
		final List<Tb_File> list=new ArrayList<Tb_File>();
		//读取图片
		final LobHandler lobHandler=new DefaultLobHandler();  
		
		this.getJdbcTemplate().query(new SQLProxy(QUERY_GET_TB_FILE.toString()).sql(), new RowMapper() {

		                public Object mapRow(ResultSet rs, int i) throws SQLException {

	                        Tb_File obj=new Tb_File();
	                        String extenName=rs.getString(3);//获取扩展名
	                        String myFileName="未知文件";//文件名
	                        
	                      	myFileName=attributeService.getFileSeq("p")+extenName;
	                 
	                        String myFilePath=ServletActionContext.getServletContext().getRealPath("downloadtemp")//获取上传路径
	              		  +"/"+myFileName;
							try {
								OutputStream os = new FileOutputStream(new File(myFilePath));
								try {
									FileCopyUtils.copy(lobHandler.getBlobAsBinaryStream(rs,2),os);
									os.close();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} 
							} catch (FileNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}//写文件
	                        
	                        obj.setId(rs.getString(1));
	                        obj.setImagesuffix(rs.getString(3));
	                        obj.setName(rs.getString(4));
	                        obj.setImage("http://localhost:8090/houseInfo/downloadtemp/"+myFileName);
	                        list.add(obj);
	                        return list;
			         }
		       });

		logger.debug(strMethod + "End.");
		return list;
	}

?