gpt4 book ai didi

php - 在Android中通过文件(使用FileInputStream)将图像发送到PHP并通过Blob保存mysql

转载 作者:行者123 更新时间:2023-11-29 16:49:40 26 4
gpt4 key购买 nike

I'm trying to send an image converted by file and fileinputstream with my android app for my PHP server and then save it in BLOB field in MySQL DB, but I tried too many things, but nothing is ok. What can I do? And so, is this possible? Help, please!

我的 PHP 服务器接收 json 并像平常一样保存其他字段,但图像不像我使用 java NetBeans 那样保存在 blob 字段中。

public void cadastrarTAG(ModeloTAG tag) throws JSONException {

FileWriter writeFile = null;

JSONObject json = new JSONObject();

json.put("tag", String.valueOf(tag.getTag()));
json.put("equipamento", String.valueOf(tag.getEquipamento()));
json.put("data_registro", String.valueOf(tag.getData_registro()));
json.put("login", String.valueOf(tag.getLogin()));
json.put("descricao", String.valueOf(tag.getDescricao()));
json.put("obs", String.valueOf(tag.getObs()));
json.put("total_manutencoes", 0);
json.put("setor", String.valueOf(tag.getSetor()));
json.put("imagemFileLength", tag.getFile().length()); Field file length
json.put("imagemFIS", tag.getFis()); field fileinputstream


try {
writeFile = new FileWriter(new File(android.os.Environment.getExternalStorageDirectory(), "saida.json"));
writeFile.write((json.toString()));
writeFile.flush();
writeFile.close();

} catch(Exception e){
System.err.println("ERRO-> "+e);
//e.printStackTrace();
}

try {
log.geraLog("Cadastro de TAG ("+tag.getSetor()+") (MOBILE)", "PINS", (String) Login.rotinas[1], md.getDataHora());
enviaJson.enviaJsonGravar(arquivoPHP, json);
} catch (Exception e) {
e.printStackTrace();
}

}

我的 PHP 服务器接收 json 并像平常一样保存其他字段,但图像不像我使用 java NetBeans 那样保存在 blob 字段中。

最佳答案

public function salvaImagem($conn, $json, $cod) {
$flag['code'] = 0;
echo "imagemFileLength 3 ---->>>>> ".$json->{'imagemFileLength'};
echo "imagemFIS 3 ---->>>>> ".$json->{'imagemFIS'};

$stmt = $conn->prepare("update tag set imagem = ? where id = ".$cod." and login = '{$json->{'login'}}'");
$imageContent = fread($json->{'imagemFIS'}, filesize($json->{'imagemFileLength'}));
//$imageContent = mysqli_real_escape_string($conn, $imageContent);
//$stmt->bind_param('s', mysql_real_escape_string($conn, $json->{'imagemFIS'}));
$stmt->bind_param('s', $imageContent);
//$stmt->bindValue(1, $json->{'imagemFIS'});
$stmt->execute();

if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Unable to create record";


$stmt->close();
$conn->close();
}

关于php - 在Android中通过文件(使用FileInputStream)将图像发送到PHP并通过Blob保存mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com