使用php和android将图像上传到Mysql服务器

时间:2019-03-18 16:50:15

标签: php android mysql

我试图建立一个注册页面,用户可以在其中注册他们的姓名和其他详细信息。输入的详细信息可以上传到我的数据库。但是我无法将图像上传到数据库。

php代码

private static Cache<String, Integer> cache;

private static final String QUERY = "EXEC myQuery";

public static Integer getLanguageId(String language) {
    if (language == null) {
        return null;
    }
    Integer languageId = cache.get(language);
    if (languageId == null) {
        throw new Exception();
    }
    return languageId;
}

static void configureCache() {
    if (cache == null) {
        //CacheManager call database
        cache = CacheManager.getInstance()
                .createCache(QUERY, new RuleConfigurer.StringAndIntegerRowMapper());
    }
}

Java代码

RequestHandler.Java

 <?php


$hostname = "localhost";
//Define your database username here.
$username = "Droidparkz";
//Define your database password here.
$password = "***********";
//Define your database name here.
$dbname = "KEA";

$con = mysqli_connect($hostname,$username,$password,$dbname);

 $Image = $_POST['Image'];

 $id = 0;

 $res = mysqli_query($con,$sql);

 while($row = mysqli_fetch_array($res)){
 $id = $row['id'];
 }

 $path = "http://droidparkz.in/kea/profileimages/images/$id.png";

 $sql = "INSERT INTO User_details (IMAGE) VALUES ('$path')";

 if(mysqli_query($con,$sql)){
 file_put_contents($path,base64_decode($image));
 echo "Successfully Uploaded";
 }

 mysqli_close($con);

Userimage.Java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;


public class RequestHandler {

    public String sendGetRequest(String uri) {
        try {
            URL url = new URL(uri);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String result;

            StringBuilder sb = new StringBuilder();

            while((result = bufferedReader.readLine())!=null){
                sb.append(result);
            }

            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public String sendPostRequest(String requestURL,
                                  HashMap<String, String> postDataParams) {

        URL url;
        String response = "";
        try {
            url = new URL(requestURL);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);


            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                response = br.readLine();
            } else {
                response = "Error Registering";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }
}

通过参考一些在线教程和其他地方来完成代码。我不确定代码是否正确。如果您提供用于上传图像和文本的单个php代码,将为您提供很大的帮助。

0 个答案:

没有答案