php - 使用 JSON 将图像文件从 MySql 发送到 android。- 文件未通过

标签 php android mysql json

**此问题涉及从 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。 我搜索了很多,但找不到任何线索。你能指出我错在哪里吗?

最佳答案

替代方法可以是

方法一:

使用图像路径,因为博客数据有时可能足够大,足以进入字符串变量,因此可能会损坏,因为您在字符串变量中收到了完整的博客...

方法2:

使用lib直接上传图片到服务器

小代码 fragment :

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();

Link

关于php - 使用 JSON 将图像文件从 MySql 发送到 android。- 文件未通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759349/

相关文章:

php - 将数据插入表中的最用户友好的方式

javascript - 使用 AJAX 和 PHP 将数据插入 MySQL

php - 在 PHP 中,太多的赋值会被认为是不好的吗?如果是这样为什么?

android - 获取 "android.content.res.resources$notfoundexception"

android - 通过 Android 应用程序在用户的 friend facebook 墙上发帖

PHP 表单使用动态输入插入数据数组

PHP 设置并读取 Cookie 字符串

android - 主 Activity 名称必须是 .MainActivity 吗?

php - 跨服务器保留经过身份验证的用户

mysql - 需要在 MySQL 中选择日期时间范围