Java 将文件发送到PHP
在我的Android Studio代码中,我打开相机拍摄一张可以保存的照片。然而,我想把这个JPG图片发送到我的PHP代码中。我对它进行了排序,以便将字符串发送到PHP,但是当我尝试发送图像时,它不起作用。正因为如此,我将图像编码为base64字符串,尝试以这种方式发送,但仍然无法正常工作 Java代码:Java 将文件发送到PHP,java,python,php,android,android-studio,Java,Python,Php,Android,Android Studio,在我的Android Studio代码中,我打开相机拍摄一张可以保存的照片。然而,我想把这个JPG图片发送到我的PHP代码中。我对它进行了排序,以便将字符串发送到PHP,但是当我尝试发送图像时,它不起作用。正因为如此,我将图像编码为base64字符串,尝试以这种方式发送,但仍然无法正常工作 Java代码: public class celebs extends AsyncTask<String, String, String> { @Override protecte
public class celebs extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Toast.makeText(getBaseContext(),s,Toast.LENGTH_SHORT).show();
}
@RequiresApi(api = Build.VERSION_CODES.O)
@SuppressLint("WrongThread")
@Override
protected String doInBackground(String... params) {
sharedPreferenceConfig = new SharedPreferenceConfig(getApplicationContext());
Intent intent = getIntent();
File Image = (File) intent.getSerializableExtra("Image");
String encode = encodeFileToBase64Binary(Image);
try {
String data = URLEncoder.encode("Image", "UTF-8") + "=" +
URLEncoder.encode(String.valueOf(Image), "UTF-8");
//URL url = new URL("http://192.168.43.35:80/App/pythonconnect.php?" + data);
URL url = new URL("http://10.167.120.26/App/pythonconnect.php?" + encode);
System.out.println("1");
URLConnection con = url.openConnection();
System.out.println("2");
con.setDoOutput(true);
System.out.println("3");
BufferedReader read = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder s = new StringBuilder();
String line = null;
while ((line = read.readLine()) != null) {
s.append(line);
}
final String result = s.toString();
System.out.println(result);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static String encodeFileToBase64Binary(File file){
String encodedfile = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
encodedfile = Base64.getEncoder().encodeToString(bytes);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedfile;
}
我的php代码:
<?php
if (isset($_POST["Image"])) {
"include connection.php";
$image = $_POST["Image"];
$message = exec("/Applications/XAMPP/xamppfiles/htdocs/App/database.py 2>&1 '$imagestr'");
echo($message);
}
?>
以base64编码字符串的形式发送图像不是理想的方法,因为GET和POST在其请求中有字符限制。这将导致413请求实体太大错误
将图像发送到服务器的最佳方法是使用来自Android的多部分请求
看看这个以base64编码字符串的形式发送图像的方法并不理想,因为GET和POST在其请求中有字符限制。这将导致413请求实体太大错误
将图像发送到服务器的最佳方法是使用来自Android的多部分请求
看看这个如果要使用POST方法并将其作为POST参数发送,base64将无法正确接收,因为它的url编码不正确,如果使用POST,则无需担心这一点。POST通常用于以任何方式存储。如果您也显示了PHP代码,那么它可能会对我们有所帮助。如果您想使用POST方法并将其作为POST参数发送,您的base64将无法正确接收,因为它的url编码不正确。如果您使用POST,则无需担心这一点。POST通常用于存储任何内容。如果您也展示了PHP代码,它也可能对我们有所帮助
<?php
if (isset($_POST["Image"])) {
"include connection.php";
$image = $_POST["Image"];
$message = exec("/Applications/XAMPP/xamppfiles/htdocs/App/database.py 2>&1 '$imagestr'");
echo($message);
}
?>