Server: (auth variable is the user login status verification, this part of the confirmation 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.J PG ";p rivate String Srcpath = environment.getexternalstoragedirectory () +"/"+ PackageName +"/"+" images "+"/"+" image. JPG ";p rivate String ActionURL = Gettargeturl (); PHP connection private void UploadFile (final String uploadurl) {thread thread = new Thread () {@Override public void Run () {//debug File File = new file (uploadfile); if (file.exists ()) {log.i ("DEBUG", "file to upload exists"); } String end = "\ r \ n"; String Twohyphens = "--"; String boundary = "******"; try {URL url = new URL (uploadurl); HttpURLConnection httpurlconnection = (httpurlconnection) url.openconnection (); Set the stream size for each transfer, which effectively prevents the phone from running out of memory crashes//This method is used to enable HTT without internal buffering when the content length is not known beforehandP the stream of the request body. Httpurlconnection.setchunkedstreamingmode (* +),//128K//Allow input/output stream Httpurlconn Ection.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.setrequestproperty ("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 fis = new FileInputStream (Srcpath); byte[] buffer = new byte[8192]; 8k int count = 0; Read file while ((count = fis.read (buffer))! =-1) {dos.write (buffer, 0, count); } fis.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 (); }
Error returned after run: (contains a link to the server receive page) The operation was aborted after the error, and no information was received from the server. java.io.filenotfoundexception:http://.../upload.php At Com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream (httpurlconnectionimpl.java:206) The line where studio indicates the error is: InputStream is = Httpurlconnection.getinputstream ();
Originally thought to be a local file does not exist, the results are confirmed by the file manager, including the code in the check after the "file exists" can be determined that the client file path is correct. Later thought the URL link has a problem, with the computer browser can open normally, can print content, so the server link should also be correct.
Baffled, search for a long time did not find a solution, special to help. Ask the old driver to take the belt.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.