使用 XAMPP 将图像上传到服务器时出现问题

Posted

技术标签:

【中文标题】使用 XAMPP 将图像上传到服务器时出现问题【英文标题】:Problem in uploading image to the server using XAMPP 【发布时间】:2019-08-15 05:37:13 【问题描述】:

我在尝试使用 xampp 将图像从应用上传到服务器时遇到问题。

问题是,当我在 php 文件上手动写入图像名称($ImageName)时,它可以正常工作,但是当我尝试使用代码时,它不会显示此消息(第一次单击底部的那个)在上传按钮和第二次点击及以上的其他消息上)。

enter image description here

这是php文件代码

<?php
require "connection.php";
if($_SERVER['REQUEST_METHOD'] == 'POST')

$DefaultId = 0;
$ImageData = $_POST['image_data'];
$ImageName =$_POST['image_tag'];  /* when I write here "stack" for example 
it works and I find the image on the path with the name stack */

$ImagePath = "Documents/$ImageName.jpg";
$ServerURL = "192.168.1.5/$ImagePath";

$InsertSQL = "INSERT INTO imageupload (image_path,image_name) values('$ServerURL','$ImageName')";

if(mysqli_query($conn, $InsertSQL))

file_put_contents($ImagePath,base64_decode($ImageData));

echo "Your Image Has Been Uploaded.";

mysqli_close($conn);
else
echo "Please Try Again";


?>

这是java代码(仅与服务器相关的代码)

public class MainActivity extends AppCompatActivity 



        class AsyncTaskUploadClass extends AsyncTask <Void,Void,String> 

            @Override
            protected void onPreExecute() 

                super.onPreExecute();

                progressDialog = ProgressDialog.show(MainActivity.this,"Image is Uploading","Please Wait",false,false);
            

            @Override
            protected void onPostExecute(String string1) 

                super.onPostExecute(string1);

                progressDialog.dismiss();

                Toast.makeText(MainActivity.this,string1,Toast.LENGTH_LONG).show();

            

            @Override
            protected String doInBackground(Void... params) 

                ImageProcessClass imageProcessClass = new ImageProcessClass();

                HashMap<String,String> HashMapParams = new HashMap<String,String>();

                HashMapParams.put("image_tag", GetImageNameFromEditText);

                HashMapParams.put("image_data", ConvertImage);

                String FinalData = imageProcessClass.ImageHttpRequest("http://192.168.1.5/documentupload.php", HashMapParams);

                return FinalData;
            
        
        AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
        AsyncTaskUploadClassOBJ.execute();
    

    public class ImageProcessClass

        public String ImageHttpRequest(String requestURL,HashMap<String, String> PData) 

            StringBuilder stringBuilder = new StringBuilder();

            try 
                url = new URL(requestURL);

                httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setReadTimeout(20000);

                httpURLConnection.setConnectTimeout(20000);

                httpURLConnection.setRequestMethod("POST");

                httpURLConnection.setDoInput(true);

                httpURLConnection.setDoOutput(true);

                outputStream = httpURLConnection.getOutputStream();

                bufferedWriter = new BufferedWriter(

                        new OutputStreamWriter(outputStream, "UTF-8"));

                bufferedWriter.write(bufferedWriterDataFN(PData));

                bufferedWriter.flush();

                bufferedWriter.close();

                outputStream.close();

                RC = httpURLConnection.getResponseCode();

                if (RC == HttpsURLConnection.HTTP_OK) 

                    bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

                    stringBuilder = new StringBuilder();

                    String RC2;

                    while ((RC2 = bufferedReader.readLine()) != null)

                        stringBuilder.append(RC2);
                    
                

             catch (Exception e) 
                e.printStackTrace();
            
            return stringBuilder.toString();
        

        private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException 

            stringBuilder = new StringBuilder();

            for (Map.Entry<String, String> KEY : HashMapParams.entrySet()) 
                if (check)
                    check = false;
                else
                    stringBuilder.append("&amp;");

                stringBuilder.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));

                stringBuilder.append("=");

                stringBuilder.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));
            

            return stringBuilder.toString();
        

    

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 5) 
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                // Now user should be able to use camera

            
            else 

                Toast.makeText(MainActivity.this, "Unable to use Camera..Please Allow us to use Camera", Toast.LENGTH_LONG).show();

            
        
    

最后是 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:visibility="visible"
    android:layout_
    android:layout_
    android:paddingBottom="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="10dp"
    android:background="#E0E0E0"
    android:focusable="true"
    android:focusableInTouchMode="true">
    <ScrollView
        android:layout_
        android:layout_>
        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:id="@+id/linearLayout">

            <TextView
                android:layout_
                android:layout_
                android:text="Upload Image To Server"
                android:textSize="26dp"
                android:textColor="#000"
                android:fontFamily="sans-serif"
                android:paddingBottom="4dp"/>
            <RelativeLayout
                android:layout_
                android:layout_
                android:background="#000"
                android:layout_marginBottom="10dp">
            </RelativeLayout>

            <TextView
                android:layout_
                android:layout_
                android:text="Image Name"
                android:layout_marginTop="10dp"/>
            <EditText
                android:layout_
                android:layout_
                android:id="@+id/imageName"/>
            <Button
                android:layout_
                android:layout_
                android:text="Select Image"
                android:id="@+id/buttonSelect"
                android:background="#FFF"
                android:layout_margin="10dp"/>

            <ImageView
                android:layout_
                android:layout_
                android:id="@+id/imageView"/>
            <RelativeLayout
                android:layout_
                android:layout_
                android:background="#000"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp">
            </RelativeLayout>

            <LinearLayout
                android:layout_
                android:layout_
                android:orientation="horizontal"
                android:gravity="right">

                <Button
                    tools:visibility="visible"
                    android:layout_
                    android:layout_
                    android:text="Upload >>"
                    android:textColor="#FFF"
                    android:background="@color/colorPrimary"
                    android:id="@+id/buttonUpload"
                    android:visibility="gone"/>

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

【问题讨论】:

这里有很多代码。如果按照***.com/help/how-to-ask 将其编辑为最小可重现示例,您将获得更好的答案 我编辑了帖子,仍然只有负责操作的asynktask代码。 谁能帮帮我。 【参考方案1】:
for (Map.Entry<String, String> KEY : HashMapParams.entrySet()) 
    if (check)
        check = false;
    else
        stringBuilder.append("&");//just give & symbol and remove amp;
    stringBuilder.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));
    stringBuilder.append("=");
    stringBuilder.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));

【讨论】:

只需在for循环中做这个简单的更改,同时将数据写入服务器 stringBuilder.append("&");//只需给出&符号并删除amp;

以上是关于使用 XAMPP 将图像上传到服务器时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试将文件(图像)上传到服务器时出现错误 403

从 iphone 设备上传图像时出现图像方向问题

上传图片到服务器时出现绿线

上传图片到服务器时出现绿线

使用 Laravel 8 将图像上传到存储和数据库时出现问题

使用 AlamoFire 和预签名 URL 将图像上传到 S3 存储桶时出现问题