有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java上传图像失败

我目前正在尝试使用Java函数将所选图像上载到我的xampp服务器。 但是,即使图像根本没有上传,该函数也会显示“上传”的祝酒词。我试图更改IP地址以查看是否显示错误,但函数仍然返回:“上传”

Java函数:

public class UploadImage extends AsyncTask<Void, Void, Void> {

    private final MainActivity mainActivity;
    Bitmap image;
    String name;

    public UploadImage(MainActivity mainActivity, Bitmap image, String name) {
        this.mainActivity = mainActivity;
        this.image = image;
        this.name = name;
        Toast.makeText(mainActivity, "Uploading...", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostExecute(Void unused) {
        Toast.makeText(mainActivity, "uploaded", Toast.LENGTH_SHORT).show();
        super.onPostExecute(unused);
    }

    @Override
    protected Void doInBackground(Void... voids) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);

        ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
        dataToSend.add(new BasicNameValuePair("image", encodedImage));
        dataToSend.add(new BasicNameValuePair("name", name));

        HttpParams httpRequestParams = getHttpRequestParams();

        HttpClient client = new DefaultHttpClient(httpRequestParams);

        HttpPost post = new HttpPost("http://my-ip/SavePicture.php");

        try {

            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private HttpParams getHttpRequestParams() {
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, 30000);
        HttpConnectionParams.setSoTimeout(httpRequestParams, 30000);
        return httpRequestParams;
    }


}

Php:

<?php
$name = $_POST["name"]
$image = $_POST["image"]

$decodedimage = base64_decode("$image")
file_put_contents("profile_pictures/".$name.".JPG", $decodedimage)

?>

共 (0) 个答案