Android 拍照上传与选择文件上传至服务器

Android 拍照上传与选择文件上传至服务器

源码下载: 

https://github.com/kkman2008/androidbrowseuploadfile

项目演示及讲解 

优酷  http://v.youku.com/v_show/id_XODk5NjkwOTg4.html​

爱奇艺  http://www.iqiyi.com/w_19rs1v2m15.html#vfrm=8-7-0-1

土豆 http://www.tudou.com/programs/view/fv0H93IHfhM

​大家在调试的同时一定要注意

1、网络下载、上传这些操作,就是耗时的操作,要另外开线程操作,不能放到主线程里面。

2、网络下载、上传里面不能有ui操作,不能在里面显示ui。就是不能在子线程里面操作UI。如果操作完毕ui提示用户等操作,可以使用利用handler结合Thread更新UI,或者AsyncTask异步更新UI。

3、上传到本地Tomcat服务器,要关闭防火墙

接下来将给出两个项目部分代码,当然两个项目都有一个工具类HttpPost

 
  1. public class HttpPost {

  2. /**

  3. * 通过拼接的方式构造请求内容,实现参数传输以及文件传输

  4. *

  5. * @param acti

  6. * .nUrl

  7. * @param params

  8. * @param files

  9. * @return

  10. * @throws IOException

  11. */

  12. public static String post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException {

  13.  
  14. String BOUNDARY = java.util.UUID.randomUUID().toString();

  15. String PREFIX = "--", LINEND = "\r\n";

  16. String MULTIPART_FROM_DATA = "multipart/form-data";

  17. String CHARSET = "UTF-8";

  18.  
  19. URL uri = new URL(actionUrl);

  20. HttpURLConnection conn = (HttpURLConnection) uri.openConnection();

  21. conn.setReadTimeout(5 * 1000); // 缓存的最长时间

  22. conn.setDoInput(true);// 允许输入

  23. conn.setDoOutput(true);// 允许输出

  24. conn.setUseCaches(false); // 不允许使用缓存

  25. conn.setRequestMethod("POST");

  26. conn.setRequestProperty("connection", "keep-alive");

  27. conn.setRequestProperty("Charsert", "UTF-8");

  28. conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

  29.  
  30. // 首先组拼文本类型的参数

  31. StringBuilder sb = new StringBuilder();

  32. for (Map.Entry<String, String> entry : params.entrySet()) {

  33. sb.append(PREFIX);

  34. sb.append(BOUNDARY);

  35. sb.append(LINEND);

  36. sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);

  37. sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);

  38. sb.append("Content-Transfer-Encoding: 8bit" + LINEND);

  39. sb.append(LINEND);

  40. sb.append(entry.getValue());

  41. sb.append(LINEND);

  42. }

  43.  
  44. DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());

  45. outStream.write(sb.toString().getBytes());

  46. // 发送文件数据

  47. if (files != null)

  48. for (Map.Entry<String, File> file : files.entrySet()) {

  49. StringBuilder sb1 = new StringBuilder();

  50. sb1.append(PREFIX);

  51. sb1.append(BOUNDARY);

  52. sb1.append(LINEND);

  53. sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getKey() + "\"" + LINEND);

  54. sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);

  55. sb1.append(LINEND);

  56. outStream.write(sb1.toString().getBytes());

  57.  
  58. InputStream is = new FileInputStream(file.getValue());

  59. byte[] buffer = new byte[1024];

  60. int len = 0;

  61. while ((len = is.read(buffer)) != -1) {

  62. outStream.write(buffer, 0, len);

  63. }

  64.  
  65. is.close();

  66. outStream.write(LINEND.getBytes());

  67. }

  68.  
  69. // 请求结束标志

  70. byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();

  71. outStream.write(end_data);

  72. outStream.flush();

  73. // 得到响应码

  74. int res = conn.getResponseCode();

  75. InputStream in = conn.getInputStream();

  76. if (res == 200) {

  77. int ch;

  78. StringBuilder sb2 = new StringBuilder();

  79. while ((ch = in.read()) != -1) {

  80. sb2.append((char) ch);

  81. }

  82. }

  83. outStream.close();

  84. conn.disconnect();

  85. return in.toString();

  86.  
  87. }

  88.  
  89. }

这个工具类两个项目所共有

接下来是拍照上传至服务器的主要代码(界面代码不再给出)

 
  1. public class MainActivity extends Activity {

  2.  
  3. private static final int PHOTO_CAPTURE = 0x11;

  4. private static String photoPath = "/sdcard/AnBo/";

  5. private static String photoName = photoPath + "laolisb.jpg";

  6. Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.jpg"));//第二个参数是临时文件,在后面将会被修改

  7. private Button photo, sc_photo;//拍照与下载

  8. private ImageView img_photo;//显示图片

  9. //private String newName = "laoli.jpg";

  10. /*

  11. * 这里的代码应该有问题

  12. */

  13. private String uploadFile = "/sdcard/AnBo/laolisb.jpg";

  14. private String actionUrl = "http://192.168.0.104:8080/UploadPhoto1/UploadServlet";

  15. // private String actionUrl = "http://192.168.0.104:8080/File/UploadAction";

  16.  
  17. @Override

  18. protected void onCreate(Bundle savedInstanceState) {

  19. super.onCreate(savedInstanceState);

  20. setContentView(R.layout.activity_main);

  21. photo = (Button) findViewById(R.id.photo);

  22. sc_photo = (Button) findViewById(R.id.sc_photo);

  23. img_photo = (ImageView) findViewById(R.id.imt_photo);

  24.  
  25. /*

  26. * android.os.NetworkOnMainThreadException

  27. * 耗时操作,加如下代码,可在主线程中进行,但不推荐

  28. */

  29. // StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

  30. // StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());

  31. sc_photo.setOnClickListener(new Sc_photo());

  32. photo.setOnClickListener(new Photo());

  33. }

  34.  
  35. class Sc_photo implements View.OnClickListener {

  36.  
  37. @Override

  38. public void onClick(View arg0) {

  39. dialog();

  40. }

  41. }

  42.  
  43. class Photo implements View.OnClickListener {

  44.  
  45. @Override

  46. public void onClick(View v) {

  47.  
  48. Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

  49. //"/sdcard/AnBo/";

  50. File file = new File(photoPath);

  51. if (!file.exists()) { // 检查图片存放的文件夹是否存在

  52. file.mkdir(); // 不存在的话 创建文件夹

  53. }

  54. //photoPath + "laolisb.jpg"

  55. File photo = new File(photoName);

  56. imageUri = Uri.fromFile(photo);

  57. // 这样就将文件的存储方式和uri指定到了Camera应用中

  58. intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

  59. startActivityForResult(intent, PHOTO_CAPTURE);

  60. }

  61. }

  62.  
  63. @Override

  64. protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  65. // TODO Auto-generated method stub

  66. super.onActivityResult(requestCode, resultCode, data);

  67. String sdStatus = Environment.getExternalStorageState();

  68. switch (requestCode) {

  69. case PHOTO_CAPTURE:

  70. if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {

  71. Log.i("内存卡错误", "请检查您的内存卡");

  72. } else {

  73. BitmapFactory.Options op = new BitmapFactory.Options();

  74. // 设置图片的大小

  75. Bitmap bitMap = BitmapFactory.decodeFile(photoName);

  76. int width = bitMap.getWidth();

  77. int height = bitMap.getHeight();

  78. // 设置想要的大小

  79. int newWidth = 480;

  80. int newHeight = 640;

  81. // 计算缩放比例

  82. float scaleWidth = ((float) newWidth) / width;

  83. float scaleHeight = ((float) newHeight) / height;

  84. // 取得想要缩放的matrix参数

  85. Matrix matrix = new Matrix();

  86. matrix.postScale(scaleWidth, scaleHeight);

  87. // 得到新的图片

  88. bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);

  89.  
  90. // canvas.drawBitmap(bitMap, 0, 0, paint)

  91. // 防止内存溢出

  92. op.inSampleSize = 4; // 这个数字越大,图片大小越小.

  93.  
  94. Bitmap pic = null;

  95. pic = BitmapFactory.decodeFile(photoName, op);

  96. img_photo.setImageBitmap(pic); // 这个ImageView是拍照完成后显示图片

  97. FileOutputStream b = null;

  98. ;

  99. try {

  100. b = new FileOutputStream(photoName);

  101. } catch (FileNotFoundException e) {

  102. e.printStackTrace();

  103. }

  104. if (pic != null) {

  105. pic.compress(Bitmap.CompressFormat.JPEG, 50, b);

  106. }

  107. }

  108. break;

  109. default:

  110. return;

  111. }

  112. }

  113.  
  114. protected void dialog() {

  115.  
  116. AlertDialog.Builder builder = new Builder(MainActivity.this);

  117. builder.setMessage("确认上传图片吗?");

  118.  
  119. builder.setTitle("提示");

  120.  
  121. builder.setPositiveButton("确认", new OnClickListener() {

  122.  
  123. @Override

  124. public void onClick(DialogInterface dialog, int which) {

  125. new Thread(new Runnable() {

  126.  
  127. @Override

  128. public void run() {

  129. uploadPhoto();

  130. //uploadFile();

  131. }

  132. }).start();

  133. }

  134. });

  135. builder.setNegativeButton("取消", new OnClickListener() {

  136.  
  137. @Override

  138. public void onClick(DialogInterface dialog, int which) {

  139. dialog.dismiss();

  140. }

  141. });

  142. builder.create().show();

  143. }

  144.  
  145. //第二种上传方式

  146. public void uploadPhoto() {

  147.  
  148. Map<String, String> params = new HashMap<String, String>();

  149. params.put("strParamName", "strParamValue");

  150. Map<String, File> files = new HashMap<String, File>();

  151. files.put(System.currentTimeMillis()+".jpg", new File(uploadFile));//uploadFile = "/sdcard/AnBo/laolisb.jpg";

  152. try {

  153. String str = HttpPost.post(actionUrl, params, files);

  154. System.out.println("str--->>>" + str);

  155. } catch (Exception e) {

  156. }

  157. }

  158.  
  159. /* 显示Dialog的method */

  160. private void showDialog(String mess) {

  161. new AlertDialog.Builder(MainActivity.this).setTitle("提示").setMessage(mess).setNegativeButton("确定", new DialogInterface.OnClickListener() {

  162. public void onClick(DialogInterface dialog, int which) {

  163. }

  164. }).show();

  165. }

  166.  
  167. }


以上就是拍照上传至服务器的主要代码,接下来是选择文件上传至服务器主要代码

界面布局

三个xml代码给出

activity_file_upload.xml

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical" >

  6.  
  7. <LinearLayout

  8. android:layout_width="fill_parent"

  9. android:layout_height="40dp"

  10. android:layout_gravity="center"

  11. android:background="#ededed"

  12. android:gravity="center_vertical"

  13. android:paddingLeft="10dip"

  14. android:paddingRight="10dip" >

  15.  
  16. <TextView

  17. android:id="@+id/cancel"

  18. android:layout_width="wrap_content"

  19. android:layout_height="wrap_content"

  20. android:layout_weight="1"

  21. android:background="@drawable/selector_message_button"

  22. android:gravity="center"

  23. android:padding="5dip"

  24. android:text="@string/cancel_back"

  25. android:textColor="@color/tab_indicator" />

  26.  
  27. <TextView

  28. android:id="@+id/title"

  29. android:layout_width="0dip"

  30. android:layout_height="fill_parent"

  31. android:layout_weight="10"

  32. android:gravity="center"

  33. android:text="@string/upload_title"

  34. android:textColor="@color/blank" />

  35.  
  36. <TextView

  37. android:id="@+id/upload"

  38. android:layout_width="wrap_content"

  39. android:layout_height="wrap_content"

  40. android:layout_weight="1"

  41. android:background="@drawable/selector_message_button"

  42. android:gravity="center"

  43. android:padding="5dip"

  44. android:text="@string/upload_begin"

  45. android:textColor="@color/tab_indicator" />

  46. </LinearLayout>

  47.  
  48. <FrameLayout

  49. android:layout_width="match_parent"

  50. android:layout_height="match_parent"

  51. android:background="#ffffff" >

  52.  
  53. <LinearLayout

  54. android:layout_width="fill_parent"

  55. android:layout_height="fill_parent"

  56. android:gravity="center"

  57. android:orientation="vertical" >

  58.  
  59. <LinearLayout

  60. android:layout_width="fill_parent"

  61. android:layout_height="40dp"

  62. android:layout_marginLeft="10dip"

  63. android:layout_marginRight="10dip"

  64. android:gravity="center"

  65. android:orientation="horizontal" >

  66.  
  67. <TextView

  68. android:layout_width="wrap_content"

  69. android:layout_height="40dp"

  70. android:gravity="right|center_vertical"

  71. android:text="@string/file_upload" />

  72.  
  73. <EditText

  74. android:id="@+id/file_path"

  75. android:layout_width="match_parent"

  76. android:layout_height="40dp"

  77. android:focusable="false"

  78. android:focusableInTouchMode="false"

  79. android:singleLine="true"

  80. android:gravity="center_vertical"

  81. android:inputType="text" />

  82. </LinearLayout>

  83.  
  84. <Button

  85. android:id="@+id/buttonLoadPicture"

  86. android:layout_width="100dip"

  87. android:layout_height="30dip"

  88. android:layout_gravity="center"

  89. android:layout_marginBottom="10dip"

  90. android:layout_marginTop="30dip"

  91. android:background="@drawable/selector_message_button"

  92. android:text="@string/look_pictrue" >

  93. </Button>

  94. </LinearLayout>

  95.  
  96. <FrameLayout

  97. android:layout_width="match_parent"

  98. android:layout_height="match_parent"

  99. android:background="#00000000" >

  100.  
  101. <RelativeLayout

  102. android:id="@+id/show"

  103. android:layout_width="fill_parent"

  104. android:layout_height="fill_parent"

  105. android:background="#30000000"

  106. android:gravity="center"

  107. android:visibility="gone" >

  108.  
  109. <ProgressBar

  110. android:id="@+id/show_pb"

  111. android:layout_width="30dip"

  112. android:layout_height="30dip"

  113. android:indeterminateDrawable="@drawable/progressbar" />

  114. </RelativeLayout>

  115. </FrameLayout>

  116. </FrameLayout>

  117.  
  118. </LinearLayout>


activity_fileupload.xml

 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:paddingBottom="@dimen/activity_vertical_margin"

  6. android:paddingLeft="@dimen/activity_horizontal_margin"

  7. android:paddingRight="@dimen/activity_horizontal_margin"

  8. android:paddingTop="@dimen/activity_vertical_margin"

  9. tools:context=".MainActivity" >

  10.  
  11. <TextView

  12. android:id="@+id/currPath"

  13. android:layout_width="wrap_content"

  14. android:layout_height="wrap_content" />

  15.  
  16. <ListView

  17. android:id="@android:id/list"

  18. android:layout_width="match_parent"

  19. android:layout_height="match_parent"

  20. android:layout_below="@+id/currPath" >

  21. </ListView>

  22.  
  23. </RelativeLayout>


item_fileuplaod.xml

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="horizontal"

  6. android:gravity="center_vertical">

  7.  
  8. <ImageView

  9. android:id="@+id/adapter_icon"

  10. android:layout_width="wrap_content"

  11. android:layout_height="wrap_content"

  12. android:layout_margin="10dp" />

  13.  
  14. <TextView

  15. android:id="@+id/adapter_txt"

  16. android:layout_width="wrap_content"

  17. android:layout_height="wrap_content"

  18. android:layout_marginLeft="20dp"

  19. />

  20. </LinearLayout>


FileSelectActivity.class

 
  1. public class FileSelectActivity extends ListActivity {

  2. private static final String root = new String(Environment

  3. .getExternalStorageDirectory().getPath() + File.separator);

  4. private TextView tv;// 显示文件的目录

  5. private File[] files;

  6.  
  7. @Override

  8. protected void onCreate(Bundle savedInstanceState) {

  9. super.onCreate(savedInstanceState);

  10. setContentView(R.layout.activity_fileupload);

  11. tv = (TextView) findViewById(R.id.currPath);

  12. getFiles(root);

  13. }

  14.  
  15. public void getFiles(String path) {

  16. tv.setText(path);

  17. File f = new File(path);

  18. // 得到所有子文件和文件夹

  19. File[] tem = f.listFiles();

  20. // 如果当前的目录不是在顶层目录,就把父目录要到files数组中的第一个

  21. if (!path.equals(root)) {

  22. files = new File[tem.length + 1];

  23. System.arraycopy(tem, 0, files, 1, tem.length);

  24. files[0] = f.getParentFile();

  25. } else {

  26. files = tem;

  27. }

  28. sortFilesByDirectory(files);

  29. // 为ListActivity设置Adapter

  30. setListAdapter(new Adapter(this, files, files.length == tem.length));

  31. }

  32.  
  33. // 对文件进行排序

  34. private void sortFilesByDirectory(File[] files) {

  35. Arrays.sort(files, new Comparator<File>() {

  36. public int compare(File f1, File f2) {

  37. return Long.valueOf(f1.length()).compareTo(f2.length());

  38. }

  39. });

  40. }

  41.  
  42. @Override

  43. protected void onListItemClick(ListView l, View v, int position, long id) {

  44. File f = files[position];

  45. if (!f.canRead()) {

  46. Toast.makeText(this, "文件不可读", Toast.LENGTH_SHORT).show();

  47. return;

  48. }

  49. if (f.isFile()) {// 为文件

  50. String path = f.getAbsolutePath();

  51. Intent intent = new Intent();

  52. intent.putExtra("path", path);

  53. setResult(FileUploadActivity.RESULT_LOAD_FILE, intent);

  54. finish();

  55. } else {

  56. getFiles(f.getAbsolutePath());

  57. }

  58. }

  59.  
  60. class Adapter extends BaseAdapter {

  61. private File[] files;

  62. private boolean istop;

  63. private Context context;

  64.  
  65. public Adapter(Context context, File[] files, boolean istop) {

  66. this.context = context;

  67. this.files = files;

  68. this.istop = istop;

  69. }

  70.  
  71. @Override

  72. public int getCount() {

  73. return files.length;

  74. }

  75.  
  76. @Override

  77. public Object getItem(int position) {

  78. return files[position];

  79. }

  80.  
  81. @Override

  82. public long getItemId(int position) {

  83. return position;

  84. }

  85.  
  86. @Override

  87. public View getView(int position, View convertView, ViewGroup parent) {

  88. Holder holder = null;

  89. if (convertView == null) {

  90. holder = new Holder();

  91. convertView = View.inflate(context, R.layout.item_fileupload,

  92. null);

  93. holder.iv = (ImageView) convertView

  94. .findViewById(R.id.adapter_icon);

  95. holder.tv = (TextView) convertView

  96. .findViewById(R.id.adapter_txt);

  97. convertView.setTag(holder);

  98. } else {

  99. holder = (Holder) convertView.getTag();

  100. }

  101. // 设置convertView中控件的值

  102. setconvertViewRow(position, holder);

  103. return convertView;

  104. }

  105.  
  106. private void setconvertViewRow(int position, Holder holder) {

  107. File f = files[position];

  108. holder.tv.setText(f.getName());

  109. if (!istop && position == 0) {// 不是在顶层目录

  110. // 加载后退图标

  111. holder.iv.setImageResource(R.drawable.back_up);

  112. } else if (f.isFile()) {// 是文件

  113. // 加载文件图标

  114. holder.iv.setImageResource(R.drawable.file);

  115. } else {// 文件夹

  116. // 加载文件夹图标

  117. holder.iv.setImageResource(R.drawable.dir);

  118. }

  119. }

  120.  
  121. class Holder {

  122. private ImageView iv;

  123. private TextView tv;

  124. }

  125. }

  126. }


FileUploadActivity.class

 
  1. public class FileUploadActivity extends Activity implements OnClickListener {

  2.  
  3. protected static final int SUCCESS = 2;

  4. protected static final int FAILD = 3;

  5. protected static int RESULT_LOAD_FILE = 1;

  6. private TextView cancel;

  7. private TextView upload;

  8. private EditText pathView;

  9. private Button buttonLoadImage;

  10. private String picturePath;

  11. private View show;

  12.  
  13. @Override

  14. public void onCreate(Bundle savedInstanceState) {

  15. super.onCreate(savedInstanceState);

  16. setContentView(R.layout.activity_file_upload);

  17. initView();

  18. initData();

  19. }

  20.  
  21. private Handler mHandler = new Handler(new Handler.Callback() {

  22.  
  23. @Override

  24. public boolean handleMessage(Message msg) {

  25. switch (msg.what) {

  26.  
  27. case SUCCESS:

  28. show.setVisibility(View.INVISIBLE);

  29. picturePath = "";

  30. pathView.setText(picturePath);

  31. Toast.makeText(getApplicationContext(), "上传成功!", Toast.LENGTH_LONG).show();

  32. break;

  33. case FAILD:

  34. show.setVisibility(View.INVISIBLE);

  35. Toast.makeText(getApplicationContext(), "上传失败!", Toast.LENGTH_LONG).show();

  36. break;

  37. default:

  38. break;

  39. }

  40. return false;

  41. }

  42.  
  43. });

  44.  
  45. private void initView() {

  46. cancel = (TextView) findViewById(R.id.cancel);

  47. upload = (TextView) findViewById(R.id.upload);

  48. buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);

  49. cancel.setOnClickListener(this);

  50. upload.setOnClickListener(this);

  51. buttonLoadImage.setOnClickListener(this);

  52. show = findViewById(R.id.show);

  53. pathView = (EditText) findViewById(R.id.file_path);

  54. pathView.setKeyListener(null);

  55. }

  56.  
  57. private void initData() {

  58. picturePath = "";

  59. }

  60.  
  61. @Override

  62. public void onClick(View v) {

  63. switch (v.getId()) {

  64. case R.id.cancel:

  65. finish();

  66. break;

  67. case R.id.buttonLoadPicture:

  68. Intent intent = new Intent(getApplicationContext(), FileSelectActivity.class);

  69.  
  70. startActivityForResult(intent, RESULT_LOAD_FILE);

  71. break;

  72. case R.id.upload:

  73. uploadFile();

  74. break;

  75. default:

  76. break;

  77. }

  78. }

  79.  
  80. @Override

  81. protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  82. super.onActivityResult(requestCode, resultCode, data);

  83. if (requestCode == RESULT_LOAD_FILE && resultCode == RESULT_LOAD_FILE && data != null) {

  84. picturePath = data.getStringExtra("path");

  85. pathView.setText(picturePath);

  86. }

  87. }

  88. /******************************************************/

  89. private String fun(String msg){

  90. int i = msg.length();

  91. int j = msg.lastIndexOf("/") + 1;

  92.  
  93. String a = msg.substring(j, i) ;

  94. System.out.println(a);

  95. return a;

  96. }

  97. /******************************************************/

  98.  
  99. private void uploadFile() {

  100. show.setVisibility(View.VISIBLE);

  101. new Thread() {

  102. @Override

  103. public void run() {

  104.  
  105. Message msg = Message.obtain();

  106.  
  107. // 服务器的访问路径

  108. String uploadUrl = "http://192.168.0.104:8080/UploadPhoto1/UploadServlet";

  109. Map<String, File> files = new HashMap<String, File>();

  110.  
  111. String name = fun(picturePath);

  112. files.put(name, new File(picturePath));

  113. //files.put("test.jpg", new File(picturePath));

  114. Log.d("str--->>>", picturePath);

  115. try {

  116. String str = HttpPost.post(uploadUrl, new HashMap<String,String>(), files);

  117. System.out.println("str--->>>" + str);

  118. msg.what = SUCCESS;

  119. } catch (Exception e) {

  120. msg.what = FAILD;

  121. }

  122. mHandler.sendMessage(msg);

  123.  
  124. }

  125. }.start();

  126. }

  127.  
  128. @Override

  129. protected void onDestroy() {

  130. show.setVisibility(View.INVISIBLE);

  131. super.onDestroy();

  132. }

  133. }

猜你喜欢

转载自blog.csdn.net/kingmax54212008/article/details/82254578
今日推荐