尝试使用xampp将图像从应用程序上传到服务器时遇到问题。
问题是,当我在PHP文件上手动写入图像名称($ ImageName)时,它可以正常工作,但是当我尝试使用代码时,它并不会显示此消息(第一次单击时位于底部的消息)点击上传按钮上的第二条消息,然后点击第二次以上)。
这是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("&");
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_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:background="#E0E0E0"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload Image To Server"
android:textSize="26dp"
android:textColor="#000"
android:fontFamily="sans-serif"
android:paddingBottom="4dp"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginBottom="10dp">
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Name"
android:layout_marginTop="10dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageName"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Image"
android:id="@+id/buttonSelect"
android:background="#FFF"
android:layout_margin="10dp"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="270dp"
android:id="@+id/imageView"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<Button
tools:visibility="visible"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Upload >>"
android:textColor="#FFF"
android:background="@color/colorPrimary"
android:id="@+id/buttonUpload"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:1)
<form method="POST" id="online_show_upload" class="online_show_upload upload_video" enctype="multipart/form-data" action="https://example.com/upload">
<label id="uploadlabel" for="upload_file">Upload File</label>
<input type="file" name="file_data" id="upload_file">
<input type="submit" value="Upload Now">
</form>