【问题标题】:Can't write image file on actual server无法在实际服务器上写入图像文件
【发布时间】:2026-01-25 09:50:02
【问题描述】:

我正在使用 android 并尝试创建一个能够将多个图像上传到服务器的应用程序。我曾尝试使用 xampp 将图像上传到我的本地主机,效果很好。但是当我尝试上传到我的企业服务器时,我找不到我的文件,换句话说。无法写入文件。不知道是什么原因导致失败?这是我的代码

上传 tp XAMPP

连接字符串private static final String url_photo = "http://192.168.7.110/blabla/base.php";

路径static final String path = "C:\\xampp\\htdocs\\psn_asset_oracle\\Images\\";

上传到实际的企业服务器

连接字符串private static final String url_photo = "http://192.168.4.27/oracle/logam/am/data_images/android_image/base.php";

路径static final String path = "http://192.168.4.27/oracle/logam/am/data_images/android_image/";

我要上传到服务器的代码

params_p.add(new BasicNameValuePair("image_name_1",
                        image_name_1));
                params_p.add(new BasicNameValuePair("image_name_2",
                        image_name_2));
                params_p.add(new BasicNameValuePair("image_name_3",
                        image_name_3));
                params_p.add(new BasicNameValuePair("image_name_4",
                        image_name_4));
json_photo = jsonParser.makeHttpRequest(url_photo, "POST", params_p);
ArrayList<NameValuePair> params_p = new ArrayList<NameValuePair>();

PHP 代码

if(isset($_POST["image_name_1"]) && isset($_POST["image_name_2"]) && isset($_POST["image_name_3"]) && isset($_POST["image_name_4"]) 
&& isset($_POST["image_1"]) && isset($_POST["image_2"]) && isset($_POST["image_3"]) && isset($_POST["image_4"]))
{
$image_name_1 = $_POST["image_name_1"];
$image_name_2 = $_POST["image_name_2"];
$image_name_3 = $_POST["image_name_3"];
$image_name_4 = $_POST["image_name_4"];
$image_1 = $_POST["image_1"];
$image_2 = $_POST["image_2"];
$image_3 = $_POST["image_3"];
$image_4 = $_POST["image_4"];

/*---------base64 decoding utf-8 string-----------*/
$binary_1=base64_decode($image_1);
$binary_2=base64_decode($image_2);
$binary_3=base64_decode($image_3);
$binary_4=base64_decode($image_4);

/*-----------set binary, utf-8 bytes----------*/
header('Content-Type: bitmap; charset=utf-8');
/*---------------open specified directory and put image on it------------------*/
$file_1 = fopen($image_name_1, 'wb');
$file_2 = fopen($image_name_2, 'wb');
$file_3 = fopen($image_name_3, 'wb');
$file_4 = fopen($image_name_4, 'wb');

/*---------------------assign image to file system-----------------------------*/
fwrite($file_1, $binary_1);
fclose($file_1);
fwrite($file_2, $binary_2);
fclose($file_2);
fwrite($file_3, $binary_3);
fclose($file_3);
fwrite($file_4, $binary_4);
fclose($file_4);

$response["message"] = "Success";
echo json_encode($response);

}

我已联系我的 DBA 并要求授予我写入文件的权限,但它仍然无法正常工作。错误是 json 没有给出“成功”作为指示文件写入失败的消息。我将不胜感激。谢谢。

【问题讨论】:

    标签: php android json image base64


    【解决方案1】:

    文件服务器是否允许写访问权限? chmod 示例,http://catcode.com/teachmod/

    您的企业服务器在线吗? IP 地址对我来说是私有的,192.168.4.27。

    【讨论】:

    • 是的,我的 dba 告诉我他们已经改变了它。当然它看起来很私密,但我成功地从该 ip 插入查询。
    【解决方案2】:

    不要使用 json 使用 http post。

    见下面代码

        HttpClient client = new DefaultHttpClient();
        String postURL = "your url";
        HttpPost post = new HttpPost(postURL);
    
        try {
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    
            ByteArrayBody bab = new ByteArrayBody(img, "image.jpg");
            reqEntity.addPart("image", bab); 
            post.setEntity(reqEntity);
            HttpResponse response = client.execute(post);
    

    这里的 img 是你的 ByteArray 格式的图片

    【讨论】:

    • 你能解释一下为什么我需要更改为多部分吗?因为我可以在我的本地服务器上使用 xampp。
    【解决方案3】:

    在我的 DBA 给我另一个文件路径后问题解决了。

    【讨论】: