mysql 二进制流数据_用mysql存储二进制数据流

/*

mysql数据库存储二进制数据 linux

用途: 用 mysql_stmt_send_long_data()来向blob字段写入2进制数据流.

注意点:需要注意的是bind结构的buffer_type字段,必须与要输入的数据类型相符,

如:只写入一个long 数据,则用MYSQL_TYPE_LONG,写入字符流,用MYSQL_TYPE_STRING,

写入2进制数据流,用MYSQL_TYPE_BLOB

具体这个参数各字段的含义参见 mysql5.0手册

Compile: g++ -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient mysql_test.cpp

准备工作:

create database test;

use test;

CREATE TABLE `bintest` (

`id` int(11) NOT NULL default '0',

`data` blob

) ENGINE=MyISAM;

*/

#include

#include

#include

#include

#define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(4, ?)"

void test()

{

MYSQL_BIND bind[1];

unsigned long length;

char blog_data[100] = {0};

memset(blog_data, 0x01, sizeof(blog_data));

char* pos = blog_data;

int size = 50;

MYSQL *mysql = mysql_init(NULL);

if (!mysql) return;

if (!mysql_real_connect(mysql,

"192.168.xx.xxx",

"root",

"db_user_name",

"test",

3306, NULL, 0))

{

int ret = mysql_errno(mysql);

mysql_close(mysql);

return;

}

MYSQL_STMT *stmt = mysql_stmt_init(mysql);

if (!stmt)

{

fprintf(stderr, " mysql_stmt_init(), out of memory/n");

exit(0);

}

if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY)))

{

fprintf(stderr, "/n mysql_stmt_prepare(), INSERT failed");

fprintf(stderr, "/n %s", mysql_stmt_error(stmt));

exit(0);

}

memset(bind, 0, sizeof(bind));

//bind[0].buffer_type= MYSQL_TYPE_STRING;

//bind[0].buffer_type = MYSQL_TYPE_LONG;

bind[0].buffer = blog_data;

//bind[0].buffer_type = MYSQL_TYPE_TINY;

bind[0].buffer_type = MYSQL_TYPE_BLOB;

bind[0].length= &length;

bind[0].is_null= 0;

/* Bind the buffers */

if (mysql_stmt_bind_param(stmt, bind))

{

fprintf(stderr, "/n param bind failed");

fprintf(stderr, "/n %s", mysql_stmt_error(stmt));

exit(0);

}

int rc =0;

/* Supply data in chunks to server */

if (mysql_stmt_send_long_data(stmt,0, pos, size))

{

fprintf(stderr, "/n send_long_data failed");

fprintf(stderr, "/n %s", mysql_stmt_error(stmt));

exit(0);

}

pos += size;

/* Supply the next piece of data */

if (mysql_stmt_send_long_data(stmt,0, pos, size))

{

fprintf(stderr, "/n send_long_data failed");

fprintf(stderr, "/n %s", mysql_stmt_error(stmt));

exit(0);

}

/* Now, execute the query */

if (mysql_stmt_execute(stmt))

{

fprintf(stderr, "/n mysql_stmt_execute failed");

fprintf(stderr, "/n %s", mysql_stmt_error(stmt));

exit(0);

}

}

int main()

{

test();

//sleep(1);

return 0;

}

在Spring Boot中,可以使用JPA或MyBatis等ORM框架来操作MySQL数据库。对于存储二进制文件,可以使用byte[]类型来存储,然后在需要还原的地方将其从数据库中读取出来。 下面是一个示例: 1. 创建一个实体类来映射数据库中的表: ```java @Entity @Table(name = "file") public class FileEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String fileName; private byte[] content; // getter and setter } ``` 2. 在Service层中定义一个方法来获取二进制文件: ```java @Service public class FileService { @Autowired private FileRepository fileRepository; public byte[] getFileContent(Long fileId) { return fileRepository.findById(fileId) .orElseThrow(() -> new RuntimeException("File not found")) .getContent(); } } ``` 3. 在Controller层中定义一个接口来返回二进制文件: ```java @RestController public class FileController { @Autowired private FileService fileService; @GetMapping("/file/{id}") public ResponseEntity<byte[]> getFile(@PathVariable Long id) { byte[] content = fileService.getFileContent(id); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "file.bin"); return new ResponseEntity<>(content, headers, HttpStatus.OK); } } ``` 4. 在Vue中,可以使用axios来调用上述接口: ```javascript axios({ method: 'get', url: '/file/' + fileId, responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'file.bin'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }); ``` 5. 在Element UI中,可以使用el-button来触发上述逻辑: ```html <template> <div> <el-button @click="downloadFile">Download File</el-button> </div> </template> <script> import axios from 'axios'; export default { name: 'DownloadFile', data() { return { fileId: 1 }; }, methods: { downloadFile() { axios({ method: 'get', url: '/file/' + this.fileId, responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'file.bin'); document.body.appendChild(link); link.click(); document.body.removeChild(link); }); } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值