Server: (the auth variable is used for user login status verification. This part is correct .) & Quot; if ($ auth1) {$ target_pathROOT.basename ($ _ FILES [& #39; uploadedfile & #39;] [& #39; name & #39;]); echo $ target_path; if (move_uploaded_file ($ _ FILES [& #39; uploadedfile & #39;] [& #39; tmp_name & #39;], $ target_path )) {echo & quot android server php Upload
Server: (the auth variable is used for user login status verification. This part is correct .)
if( $auth==1){ $target_path = ROOT.basename($_FILES['uploadedfile']['name']); echo $target_path; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploaded_file']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error']; } }else{ echo 'error_login';}
Client:
Private String uploadFile = Environment. getExternalStorageDirectory () + "/" + packageName + "/" + "images" + "/" + "image.jpg"; private String srcPath = Environment. getExternalStorageDirectory () + "/" + packageName + "/" + "images" + "/" + "image.jpg"; private String actionUrl = getTargetUrl (); // PHP connection private void uploadFile (final String uploadUrl) {Thread thread = new Thread () {@ Override public voi D run () {// DEBUG File file = new File (uploadFile); if (file. exists () {Log. I ("DEBUG", "the file to be uploaded exists");} String end = "\ r \ n"; String twoHyphens = "--"; string boundary = "*******"; try {URL url = new URL (uploadUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url. openConnection (); // sets the stream size for each transmission, this method can effectively prevent the phone from crashing due to insufficient memory // This method is used to enable the stream of HTTP request body without internal buffering when the content length is unknown in advance. HttpURLConnection. setChunkedStreamingMode (128*1024); // 128 K // allow the input/output stream httpURLConnection. setDoInput (true); httpURLConnection. setDoOutput (true); httpURLConnection. setUseCaches (false); // use the POST method httpURLConnection. setRequestMethod ("POST"); httpURLConnection. setRequestProperty ("Connection", "Keep-Alive"); httpURLConnection. setRequestProperty ("Charset", "UTF-8"); httpURLConnection. setRequestPropert Y ("Content-Type", "multipart/form-data; boundary =" + boundary); DataOutputStream dos = new DataOutputStream (httpURLConnection. getOutputStream (); dos. writeBytes (twoHyphens + boundary + end); dos. writeBytes ("Content-Disposition: form-data; name = \" uploadedfile \ "; filename = \" "+ srcPath. substring (srcPath. lastIndexOf ("/") + 1) + "\" "+ end); dos. writeBytes (end); FileInputStream FCM = new FileInputStrea M (srcPath); byte [] buffer = new byte [8192]; // 8 k int count = 0; // read the file while (count = FCM. read (buffer ))! =-1) {dos. write (buffer, 0, count);} FD. close (); dos. writeBytes (end); dos. writeBytes (twoHyphens + boundary + twoHyphens + end); dos. flush (); InputStream is = httpURLConnection. getInputStream (); InputStreamReader isr = new InputStreamReader (is, "UTF-8"); BufferedReader br = new BufferedReader (isr); String result = br. readLine (); dos. close (); is. close ();} catch (Exception e) {e. printStackTrace (); setTitle (e. getMessage () ;}}; thread. start ();}
An error is returned after running: (the link to the server receiving page is included)
After an error is reported, the operation is stopped and no message is received from the server.
Java. io. FileNotFoundException: http: //.../upload. php
At com. android. okhttp. internal. http. HttpURLConnectionImpl. getInputStream (HttpURLConnectionImpl. java: 206)
The row indicating the error in Studio is:
InputStream is = httpURLConnection. getInputStream ();
At first, I thought that the local file did not exist. The result was confirmed by the file manager and "File existence" was also displayed after checking in the code. it can be determined that the client file path is correct. Later, I thought there was a problem with the URL link. I could open it normally in a computer browser and print the content. so the server link should be correct.
I was so puzzled that I couldn't find a solution after searching for a long time. I 'd like to ask for help. Ask the old driver to take the belt.