Php 将android图像上载到mysql时出错
我从Android应用程序上传图像到MySQL服务器时出错 我遵循了一个教程,但无法完成此任务。 我使用4列的InnoDB表(id、firstname、lastname、image)。 所以,当我运行我的应用程序并开始键入我的编辑文本,然后单击“保存”时,它会工作并将数据存储到数据库中。 我的观点是:如何使用我的代码将图像上传到数据库 Signature.javaPhp 将android图像上载到mysql时出错,php,android,mysql,Php,Android,Mysql,我从Android应用程序上传图像到MySQL服务器时出错 我遵循了一个教程,但无法完成此任务。 我使用4列的InnoDB表(id、firstname、lastname、image)。 所以,当我运行我的应用程序并开始键入我的编辑文本,然后单击“保存”时,它会工作并将数据存储到数据库中。 我的观点是:如何使用我的代码将图像上传到数据库 Signature.java @Override protected Map<String,
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
File directory = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(directory, System.currentTimeMillis() + ".JPEG");
FileOutputStream out = null;
Bitmap bitmap = signatureView.getSignatureBitmap();
try {
out = new FileOutputStream(file);
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
ByteArrayOutputStream bytedata = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytedata);
byte[] data = bytedata.toByteArray();
String imagedata = Base64.encodeToString(data, Base64.DEFAULT);
String name="prescription";
} else {
throw new FileNotFoundException();
}
} catch (Exception e) {
e.printStackTrace();
}
parameters.put("firstname", txt1.getText().toString());
parameters.put("lastname", txt2.getText().toString());
return parameters;
}
};
requestQueue.add(request);
}
});
@覆盖
受保护的映射getParams()引发AuthFailureError{
映射参数=新的HashMap();
文件目录=环境
.getExternalStoragePublicDirectory(Environment.DIRECTORY\u图片);
File File=新文件(目录,System.currentTimeMillis()+“.JPEG”);
FileOutputStream out=null;
位图位图=signatureView.getSignatureBitmap();
试一试{
out=新文件输出流(文件);
if(位图!=null){
bitmap.compress(bitmap.CompressFormat.JPEG,100,out);
ByteArrayOutputStream bytedata=新建ByteArrayOutputStream();
compress(bitmap.CompressFormat.JPEG,100,bytedata);
字节[]数据=bytedata.toByteArray();
字符串imagedata=Base64.encodeToString(数据,Base64.DEFAULT);
String name=“处方”;
}否则{
抛出新的FileNotFoundException();
}
}捕获(例外e){
e、 printStackTrace();
}
parameters.put(“firstname”,txt1.getText().toString());
parameters.put(“lastname”,txt2.getText().toString());
返回参数;
}
};
添加(请求);
}
});
connection.php
<?php
define('hostname', 'localhost');
define('user', 'root');
define('password', '');
define('databaseName', 'tutorial');
$connect = mysqli_connect(hostname, user, password, databaseName);
mysqli_set_charset($connect,"utf8");
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}
function createStudent()
{
global $connect;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$query = " Insert into students(firstname,lastname) values ('$firstname','$lastname');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
insert.php
<?php
define('hostname', 'localhost');
define('user', 'root');
define('password', '');
define('databaseName', 'tutorial');
$connect = mysqli_connect(hostname, user, password, databaseName);
mysqli_set_charset($connect,"utf8");
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}
function createStudent()
{
global $connect;
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$query = " Insert into students(firstname,lastname) values ('$firstname','$lastname');";
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
}
?>
我不明白,它可以工作,数据存储到数据库中
那么它可以工作了吗?您可以接受SQL注入。是的,edittext上的文本已成功上载,但图像未上载到数据库@user3783243您只向数据库写入firstname
和lastname
。我也不确定您是否正确地从JAVA发送图像。@user3783243是的,我知道,但是我如何修改现有代码,以便在insert.php文件中上载图像。?图像是否作为JAVA的$\u文件发送?更新查询并存储映像或其在文件系统上的位置。