**这个问题是指从MySQL传递文件,而不是从URL传递文件。因此,请仔细检查您是否愿意将其标记为重复**
我试图通过php将图像(存储为BLOB)从MySQL传递给android。在PHP中我正在做:
function getimage($id)
{
$sql = "SELECT mime,data, evimagename FROM evidence
WHERE transactionID = :transactionID";
$stmt = $this->conn->prepare($sql);
$stmt->execute(array(":transactionID" => $id));
$stmt->bindColumn(1, $mime);
$stmt->bindColumn(2, $data, PDO::PARAM_LOB);
$stmt->bindColumn(3, $imgname);
$stmt->fetch(PDO::FETCH_BOUND);
return array("mime" => $mime,
"data" => $data,"imgname"=>$imgname);
}
并将此功能称为:
$result = $blobObj->selectBlob($trid);
$file= $result['data'];
header("Content-Type:application/json");
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
$filename = $a['imgname'];
header('Content-Disposition: attachment;filename='.$filename.".jpeg");
$encoded = base64_encode($file);
echo json_encode(array('image'=>$encoded));
在android中,我试图将这个json_encoded文件作为:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://10.1.1.5/connect/getevimage.php");
try {
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
String trid = arg0[0];
nameValuePairs.add(new BasicNameValuePair("TRID", trid));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream content = response.getEntity().getContent();
String responsestring = content.toString();
Log.d("chddeck",responsestring);
long filesize = response.getEntity().getContentLength();
Log.d("filesize",String.valueOf(filesize));
在我的日志中,我可以看到响应字符串为
org.apache.http.conn.EofSensorInputStream@41a2fc58
,文件大小为-1。 我搜索了很多,但无法找到任何线索。你能指出我哪里出错吗?
答案 0 :(得分:0)
替代方法可以是
方法1:
使用图像路径,因为某些博客数据可能很大程度上需要输入字符串变量,因此可能会因为你在字符串变量中收到完整的博客而被破坏...
方法2:
使用lib在服务器上直接上传图像
小代码片段:
mFtpClient.connect(InetAddress.getByName(server));
mFtpClient.login(user, password);
mFtpClient.changeWorkingDirectory(serverRoad);
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn=null;
buffIn=new BufferedInputStream(new FileInputStream(file));
mFtpClient.enterLocalPassiveMode();
mFtpClient.storeFile("test.txt", buffIn);
buffIn.close();
mFtpClient.logout();
mFtpClient.disconnect();