Android image upload to PHP Server instance background asynchronously
I have uploaded a lot of files to the java server on the Internet for a long time and found the files to be uploaded to php. The idea is similar to what I originally thought, that is, POST. Look at the code.
PHP code
PHP code
-
- $ Target_path = "./upload/"; // receiving file directory
- $ Target_path = $ target_path. basename ($ _ FILES ['uploadfile'] ['name']);
- If (move_uploaded_file ($ _ FILES ['uploadfile'] ['tmp _ name'], $ target_path )){
- Echo "The file". basename ($ _ FILES ['uploadfile'] ['name']). "has been uploaded ";
- } Else {
- Echo "There was an error uploading the file, please try again! ". $ _ FILES ['uploadfile'] ['error'];
- }
- ?>
Android code
Main Upload code:
Java code
- Private void uploadFile (String uploadUrl)
- {
- String end = "\ r \ n ";
- String twoHyphens = "--";
- String boundary = "******";
- Try
- {
- URL url = new URL (uploadUrl );
- HttpURLConnection httpURLConnection = (HttpURLConnection) url
- . OpenConnection (); // http connection
- // Set the stream size for each transmission to effectively prevent the phone from crashing due to insufficient memory
-
- HttpURLConnection. setChunkedStreamingMode (128*1024); // 128 K
- // Allow input and output streams
- HttpURLConnection. setDoInput (true );
- HttpURLConnection. setDoOutput (true );
- HttpURLConnection. setUseCaches (false );
- // Use the POST method
- HttpURLConnection. setRequestMethod ("POST ");
- HttpURLConnection. setRequestProperty ("Connection", "Keep-Alive"); // Keep the Connection
- HttpURLConnection. setRequestProperty ("Charset", "UTF-8"); // encoding
- HttpURLConnection. setRequestProperty ("Content-Type ",
- "Multipart/form-data; boundary =" + boundary); // POST the passed encoding
-
- DataOutputStream dos = new DataOutputStream (
- HttpURLConnection. getOutputStream (); // output stream
- 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 FileInputStream (srcPath); // File input stream, written to memory
- Byte [] buffer = new byte [0, 8192]; // 8 k
- Int count = 0;
- // Read the file
- While (count = fs. read (buffer ))! =-1)
- {
- Dos. write (buffer, 0, count );
- }
- FCM. close ();
-
- Dos. writeBytes (end );
- Dos. writeBytes (twoHyphens + boundary + twoHyphens + end );
- Dos. flush ();
-
- InputStream is = httpURLConnection. getInputStream (); // http input, that is, the returned result is obtained.
- InputStreamReader isr = new InputStreamReader (is, "UTF-8 ");
- BufferedReader br = new BufferedReader (isr );
- String result = br. readLine ();
-
- Toast. makeText (this, result, Toast. LENGTH_LONG). show (); // output the result
- Dos. close ();
- Is. close ();
-
- } Catch (Exception e)
- {
- E. printStackTrace ();
- SetTitle (e. getMessage ());
- }
- }
Because the time-consuming operations after Android 4.0 are required to be performed in non-UI threads, I will use the previous AsyncTask ~
AsyncTask portal: http://www.cnblogs.com/yydcdut/p/3707960.html
In this class, put the upload operation in doInBackground, and you can see ProgressDialog to display the upload quantity:
Java code
- // Read file
- BytesRead = fileInputStream. read (buffer, 0, bufferSize );
-
- While (bytesRead> 0 ){
- OutputStream. write (buffer, 0, bufferSize );
- Length + = bufferSize;
- Progress = (int) (length * 100)/totalSize );
- PublishProgress (progress );
-
- BytesAvailable = fileInputStream. available ();
- BufferSize = Math. min (bytesAvailable, maxBufferSize );
- BytesRead = fileInputStream. read (buffer, 0, bufferSize );
- }
- OutputStream. writeBytes (lineEnd );
- OutputStream. writeBytes (twoHyphens + boundary + twoHyphens
- + LineEnd );
- PublishProgress (100 );
Also, pay attention to the permissions:
XML/HTML code
- <Uses-permission android: name = "android. permission. INTERNET"/>