我阅读本教程关于在DB中存储图像.在本教程中,作者在插入之前转义二进制数据中的特殊字符:http://www.phpriot.com/articles/images-in-mysql/7 (addslashes尽管使用虽然mysql_real_escape_string更好 - 但这是另一个问题).
关键是,在显示时,他只显示存储的数据:http://www.phpriot.com/articles/images-in-mysql/8
我的问题:
1)我们是否需要转义特殊字符,即使对于二进制字段类型(blob)?
2)如果是这样,那么,为了正确显示图像,我们不需要再次"取消"字符吗?(如果是这样,最好的方法是什么.关于效率的任何评论?对于大图像:转义和转移可能是一个很大的开销?).
或者我对转义的理解是完全错误的(转义只会影响查询,而不会影响插入/存储的最终数据?).
谢谢
J.P
使用python struct module时可以指定一个格式字符串,声明如何解释二进制数据:
>>> from struct import *
>>> fmt = 'hhl'
>>> values = [1,2,3]
>>> blob = pack(fmt, values)
Run Code Online (Sandbox Code Playgroud)
可以很容易地计算存储该格式实例所需的字节数:
>>> calcsize(fmt)
Run Code Online (Sandbox Code Playgroud)
检索变量数量的最佳方法是什么?需要"填充"格式?基本上,这将预先告诉上面示例中执行pack()的'values'数组应该有多大.
>>> calcentries(fmt)
3
Run Code Online (Sandbox Code Playgroud)
有这样的事吗?
(为了论证而忽略字节序 - 这只是一个测试用例/概念证明 - 我也绝不会strcpy在实际代码中使用!)
考虑以下简单的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* variables of type message_t will be stored contiguously in memory */
typedef struct {
int message_id;
char message_text[80];
} message_t;
int main(int argc, char**argv) {
message_t* m = (message_t*)malloc(sizeof(message_t));
m->message_id = 1;
strcpy(m->message_text,"the rain in spain falls mainly on the plain");
/* write the memory to disk */
FILE* fp = fopen("data.dat", "wb");
fwrite((void*)m, sizeof(int) + strlen(m->message_text) + 1, 1, fp);
fclose(fp);
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
它写的文件很容易从磁盘中读回: …
在经过多次尝试阅读httprequest的响应后仍然苦苦挣扎,这是一个代表jpg图像的二进制数据流.
编辑:整件事
xmlhttp = false;
/*@cc_on@*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end@*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
} …Run Code Online (Sandbox Code Playgroud) 我有包含空字符的字符串即\0.如何在java中打印整个字符串?
String s = new String("abc\u0000def");
System.out.println(s.length());
System.out.println(s);
Run Code Online (Sandbox Code Playgroud)
在eclipse控制台上输出:
7
abc
Run Code Online (Sandbox Code Playgroud)
长度是完整字符串的长度,但如何打印整个字符串?
更新:我正在使用
Eclipse Helios Service Release 2
Java 1.6
从字符串解包工作:
>>> import struct
>>> struct.unpack('>h', 'ab')
(24930,)
>>> struct.unpack_from('>h', 'zabx', 1)
(24930,)
Run Code Online (Sandbox Code Playgroud)
但如果它是bytearray:
>>> struct.unpack_from('>h', bytearray('zabx'), 1)
Traceback (most recent call last):
File "<ipython-input-4-d58338aafb82>", line 1, in <module>
struct.unpack_from('>h', bytearray('zabx'), 1)
TypeError: unpack_from() argument 1 must be string or read-only buffer, not bytearray
Run Code Online (Sandbox Code Playgroud)
这似乎有点奇怪.我该怎么办呢?显然我可以:
>>> struct.unpack_from('>h', str(bytearray('zabx')), 1)
(24930,)
Run Code Online (Sandbox Code Playgroud)
但我明确地试图避免复制可能的大量内存.
我正在编写简单的php代理,我在显示png文件时遇到问题,输出是

它应该是:

图像在Notepad ++中打开.我的php curl代码如下所示:
$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
header('Content-Type:' . $info['content_type']);
echo $content
Run Code Online (Sandbox Code Playgroud)
我尝试使用和不使用CURLOPT_BINARYTRANSFER,输出相同,图像不显示.如何显示图像?
编辑:当我将数据保存到文件并使用位置标题重定向时,图像显示正确:
$file = fopen('proxy_tmp~', 'w');
fwrite($file, $content);
fclose($file);
header('Location: ' . DIR . 'proxy_tmp~');
Run Code Online (Sandbox Code Playgroud)
编辑2:我有gzip压缩,卜当我禁用它,我有同样的问题,当我在记事本中打开这两个文件++一个是DOS/Windows的ANSI(原件),另一种是DOS/Windows的UTF-8(由打开的文件一个脚本).当我在记事本中打开文件并将编码更改为ANSI并保存文件时,一切正常.
编辑3:我认为我在GNU/Linux上做了同样的事情,但没有CURLOPT_BINARYTRANSFER选项,它工作正常,这是我的项目https://github.com/jcubic/yapp.我也在Windows 10上使用Wamp进行测试,并且工作正常.
我的二进制文件包含双浮点(8字节)或浮点数(4字节),它是按以下方式生成的:
$ python -c $'from struct import pack\nwith open("file.bin", "wb") as f: f.write(pack("<d", 0.123))'
$ xxd file.bin
00000000: b072 6891 ed7c bf3f .rh..|.?
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式从Python打印出来:
$ python -c $'from struct import unpack\nwith open("file.bin", "rb") as f: print(unpack("<d", f.read(8)))'
(0.123,)
Run Code Online (Sandbox Code Playgroud)
4字节浮点数相同,只需更改<d为<f(float.bin稍后提及).
如何使用更干净的方式从shell脚本中打印该值(不使用Python)?或者使用内置工具(例如printf),或宽使用外部工具(例如xxd,dc,bc,od,hexdump,等等).
例如,要打印十进制值,我可以使用xxd(Vim的一部分),例如在Bash中:
获取第一个字节的值:
$ echo $((16#$(xxd -ps -s0 -l1 file.bin)))
176
Run Code Online (Sandbox Code Playgroud)
对于第二个和第四个字节,增加-s.
从所有8个字节中获取十进制值:
$ echo $((16#$(xxd -ps -s0 -l8 …Run Code Online (Sandbox Code Playgroud)在Swift 3.x中,我们通常使用Data; 从中你可以生成大多数其他重要类型,并且它上面有很多有用的功能.
但是我如何创建Data一个InputStream?有一个很好的方式吗?
我从位于localhost:5984/categories/Jan
以下数据的文档开始
:
{
"_id": "Jan",
"_rev": "4-2c0b1c27daca6d2a3c375b0f879a8967",
"name": "Jan",
}
Run Code Online (Sandbox Code Playgroud)
我想将pdf上传到此文档,因此我提供以下curl命令:
curl -vX PUT 'http://localhost:5984/categories/Jan/example.pdf?rev=4-2c0b1c27daca6d2a3c375b0f879a8967' -d@example.pdf -H "ContentType: application/pdf"
Run Code Online (Sandbox Code Playgroud)
我得到了这样的答复:
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5984 (#0)
> PUT /categories/Jan/example.pdf?rev=4-2c0b1c27daca6d2a3c375b0f879a8967 HTTP/1.1
> Host: localhost:5984
> User-Agent: curl/7.47.0
> Accept: */*
> ContentType: application/pdf
> Content-Length: 10944067
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< Server: MochiWeb/1.0 (Any of you quaids got a smint?)
< Date: Thu, 11 Jan 2018 07:48:21 …Run Code Online (Sandbox Code Playgroud) binary-data ×10
curl ×2
php ×2
python ×2
attachment ×1
c ×1
command-line ×1
couchdb ×1
ffi ×1
html ×1
image ×1
inputstream ×1
java ×1
javascript ×1
mysql ×1
ocaml ×1
responsetext ×1
security ×1
shell ×1
stream ×1
string ×1
struct ×1
swift ×1
xxd ×1