[Java] // some Defined variables
Private String newName = "image.jpg ";
Private String uploadFile = "/sdcard/apple.jpg ";
// Private String actionUrl = "http: // localhost: 8080/testCutPic/upload. php ";
// Upload the image address. The parameter of the uploaded image file is fileToUpload.
Private String actionUrl = "http: // 192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php ";
// Some Defined variables
Private String newName = "image.jpg ";
Private String uploadFile = "/sdcard/apple.jpg ";
// Private String actionUrl = "http: // localhost: 8080/testCutPic/upload. php ";
// Upload the image address. The parameter of the uploaded image file is fileToUpload.
Private String actionUrl = "http: // 192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php ";
HttpClient client request method:
[Html] private void postFile (){
HttpClient httpclient = new DefaultHttpClient ();
Httpclient. getParams (). setParameter (CoreProtocolPNames. PROTOCOL_VERSION, HttpVersion. HTTP_1_1 );
HttpPost httppost = new HttpPost ("http: // 192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php ");
File file = new File (uploadFile );
// File file2 = new File (uploadFile );
ContentBody cbFile = new FileBody (file, "image/jpeg ");
// ContentBody cbFile2 = new FileBody (file2, "image/jpeg ");
MultipartEntity mpEntity = new MultipartEntity ();
MpEntity. addPart ("fileToUpload", cbFile );
// MpEntity. addPart ("fileToUpload2", cbFile2 );
// MpEntity. addPart ("string parameter", new StringBody ("user "));
Httppost. setEntity (mpEntity );
Log. d ("log", "Request Information:" + httppost. getRequestLine ());
// System. out. println ("executing request" + httppost. getRequestLine ());
HttpResponse response = null;
Try {
Response = httpclient.exe cute (httppost );
} Catch (ClientProtocolException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
} Catch (IOException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
HttpEntity resEntity = response. getEntity ();
Log. d ("log", "response information:" + response. getStatusLine (). toString ());
// System. out. println (response. getStatusLine ());
If (resEntity! = Null ){
Try {
Final String response_str = EntityUtils. toString (resEntity );
Log. d ("log", response_str );
} Catch (ParseException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
If (resEntity! = Null ){
Try {
ResEntity. consumeContent ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Httpclient. getConnectionManager (). shutdown ();
}
Private void postFile (){
HttpClient httpclient = new DefaultHttpClient ();
Httpclient. getParams (). setParameter (CoreProtocolPNames. PROTOCOL_VERSION, HttpVersion. HTTP_1_1 );
HttpPost httppost = new HttpPost ("http: // 192.168.1.64/svn_ys/sousoutu/api/api_imgupload.php ");
File file = new File (uploadFile );
// File file2 = new File (uploadFile );
ContentBody cbFile = new FileBody (file, "image/jpeg ");
// ContentBody cbFile2 = new FileBody (file2, "image/jpeg ");
MultipartEntity mpEntity = new MultipartEntity ();
MpEntity. addPart ("fileToUpload", cbFile );
// MpEntity. addPart ("fileToUpload2", cbFile2 );
// MpEntity. addPart ("string parameter", new StringBody ("user "));
Httppost. setEntity (mpEntity );
Log. d ("log", "Request Information:" + httppost. getRequestLine ());
// System. out. println ("executing request" + httppost. getRequestLine ());
HttpResponse response = null;
Try {
Response = httpclient.exe cute (httppost );
} Catch (ClientProtocolException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
} Catch (IOException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
HttpEntity resEntity = response. getEntity ();
Log. d ("log", "response information:" + response. getStatusLine (). toString ());
// System. out. println (response. getStatusLine ());
If (resEntity! = Null ){
Try {
Final String response_str = EntityUtils. toString (resEntity );
Log. d ("log", response_str );
} Catch (ParseException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
If (resEntity! = Null ){
Try {
ResEntity. consumeContent ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Httpclient. getConnectionManager (). shutdown ();
} Byte stream upload:
[Html] private void uploadFile (){
String end = "\ r \ n ";
String twoHyphens = "--";
String boundary = "*****";
Try {
URL url = new URL (actionUrl );
HttpURLConnection con = (HttpURLConnection) url. openConnection ();
Con. setDoInput (true );
Con. setDoOutput (true );
Con. setUseCaches (false );
Con. setRequestMethod ("POST ");
Con. setRequestProperty ("Connection", "Keep-Alive ");
Con. setRequestProperty ("Charset", "UTF-8 ");
Con. setRequestProperty ("Content-Type ",
"Multipart/form-data; boundary =" + boundary );
DataOutputStream ds = new DataOutputStream (con. getOutputStream ());
Ds. writeBytes (twoHyphens + boundary + end );
Ds. writeBytes ("Content-Disposition: form-data ;"
+ "Name = \" file1 \ "; filename = \" "+ newName +" \ "" + end );
Ds. writeBytes (end );
FileInputStream fStream = new FileInputStream (uploadFile );
Int buffer size = 1024;
Byte [] buffer = new byte [bufferSize];
Int length =-1;
While (length = fStream. read (buffer ))! =-1 ){
Ds. write (buffer, 0, length );
}
Ds. writeBytes (end );
Ds. writeBytes (twoHyphens + boundary + twoHyphens + end );
FStream. close ();
Ds. flush ();
InputStream is = con. getInputStream ();
Int ch;
StringBuffer B = new StringBuffer ();
While (ch = is. read ())! =-1 ){
B. append (char) ch );
}
ShowDialog ("uploaded successfully" + B. toString (). trim ());
Ds. close ();
} Catch (Exception e ){
E. printStackTrace ();
ShowDialog ("Upload Failed" + e );
}
Private void uploadFile (){
String end = "\ r \ n ";
String twoHyphens = "--";
String boundary = "*****";
Try {
URL url = new URL (actionUrl );
HttpURLConnection con = (HttpURLConnection) url. openConnection ();
Con. setDoInput (true );
Con. setDoOutput (true );
Con. setUseCaches (false );
Con. setRequestMethod ("POST ");
Con. setRequestProperty ("Connection", "Keep-Alive ");
Con. setRequestProperty ("Charset", "UTF-8 ");
Con. setRequestProperty ("Content-Type ",
"Multipart/form-data; boundary =" + boundary );
DataOutputStream ds = new DataOutputStream (con. getOutputStream ());
Ds. writeBytes (twoHyphens + boundary + end );
Ds. writeBytes ("Content-Disposition: form-data ;"
+ "Name = \" file1 \ "; filename = \" "+ newName +" \ "" + end );
Ds. writeBytes (end );
FileInputStream fStream = new FileInputStream (uploadFile );
Int buffer size = 1024;
Byte [] buffer = new byte [bufferSize];
Int length =-1;
While (length = fStream. read (buffer ))! =-1 ){
Ds. write (buffer, 0, length );
}
Ds. writeBytes (end );
Ds. writeBytes (twoHyphens + boundary + twoHyphens + end );
FStream. close ();
Ds. flush ();
InputStream is = con. getInputStream ();
Int ch;
StringBuffer B = new StringBuffer ();
While (ch = is. read ())! =-1 ){
B. append (char) ch );
}
ShowDialog ("uploaded successfully" + B. toString (). trim ());
Ds. close ();
} Catch (Exception e ){
E. printStackTrace ();
ShowDialog ("Upload Failed" + e );
}
}
The dialog box used to display the test results:
[Html] private void showDialog (String mess ){
W AlertDialog. Builder (TestuploadImageActivity. this)
. SetTitle ("Message"). setMessage (mess)
. SetNegativeButton ("OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
}
}). Show ();
From agods-footprint