【发布时间】:2011-06-18 19:59:48
【问题描述】:
我有一个带有 MySQL 的远程数据库,并且我将我的应用程序用户的照片作为 LONGTEXT 类型的数据库的一行存储在数据库中。
我使用 Base64 将照片转换为字符串。
我使用 JSON 和 PHP 连接到我的远程数据库,因为这个,我必须使用 Base64,因为据我所知,JSON 和 PHP 需要在参数上发送字符串,而使用 Base64 我可以将照片转换为字符串.
它工作正常,但速度很慢。当我加载 100 KB 的照片时,需要很长时间,但当我加载 5 KB 的照片时,只需两三秒。
一位朋友告诉我使用 BLOB 而不是 Base64,但是如何将 BLOB 与 JSON 以及与数据库的 PHP 连接一起使用?另外,我需要将图像存储在表格USER 的一行中。这是因为用户没有权限将文件上传到远程服务器,但他们可以通过将照片上传为表格USER的一行中的字符串来上传照片。
谢谢
编辑:
这是等待战利品时间的代码(它在队列中等待:while ((line = reader.readLine()) != null) {,它正在等待reader.readLine())
此代码从远程数据库获取一个用户,在我的应用程序上显示该用户需要很长时间
public Friend RetrieveOneUser(String email)
{
Friend friend=null;
String result = "";
//the parameter data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email",email));
//http post
InputStream is=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.BaseURL + this.GetOneUser_URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
friend=new Friend(json_data.getString("email"),json_data.getString("password"), json_data.getString("fullName"), json_data.getString("mobilePhone"), json_data.getString("mobileOperatingSystem"),"",json_data.getString("photo"));
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return friend;
}
【问题讨论】:
-
将图像本身存储在数据库中(而不仅仅是存储对远程文件系统中文件的引用)可能是一个糟糕的举动。有一个非常好的现有问题可以详细检查这个主题:Storing Images in DB - Yea or Nay?
-
我无法在远程文件系统中存储对文件的引用,因为应用程序的用户无法将照片上传到远程系统.....我在我的问题中告诉了它¿为什么有人给我投了反对票??
-
为什么用户不能上传照片到系统?
-
+1 这仍然是一个合理的问题,您是否同意他将照片存储在数据库中并不相关。
-
您的设置似乎非常低效,这可能是它如此缓慢的原因。为什么不能保存到文件系统,然后将文件名保存到数据库中?您可以为此和 PHP $_FILES 变量使用标准 HTTP 上传。这里有很多有用的链接:stackoverflow.com/questions/2544517/…
标签: php android json blob base64