`

Web与Android 上传图片

    博客分类:
  • web
阅读更多

    Web上传图片

        Web前端html上传图片标签    

   <form id="fabu" method="post" enctype="multipart/form-data">     //表单必须设置enctype  
          
   <tr>
      <td>上传图片:</td>
       <td><input   type="file" name="uploadFile" /></td>
    </tr>

         web后台解析上传文件

        Map map=new HashMap();
        FileItemFactory factory = new DiskFileItemFactory();
	// 创建一个新的文件上传处理器
	ServletFileUpload upload = new ServletFileUpload(factory);
        List items;
	try {
		items = upload.parseRequest(request);
		Iterator iter = items.iterator();//迭代
	    while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();
		if (item.isFormField()) {  //判断该表单项是否是普通类型否则是图片类型
		     String name = item.getFieldName();
		     String value = item.getString();
		     map.put(name, value);  //将传过来的其他参数获取并保存到map
		} else {
		     processUploadedFile(item,request);
		}
	      }
	   } catch (FileUploadException e) {
			e.printStackTrace();
	   }
	     String min_num=""+map.get("minNum");
	     String content=""+ map.get("content");
	     String create_time=""+map.get("create_time");
	
	//获取图片		
        private void processUploadedFile(FileItem item,HttpServletRequest request) {
	   if (!item.isFormField()) {
//	       String fieldName = item.getFieldName();
//	       String fileName = item.getName();
//	       String contentType = item.getContentType();
//	       boolean isInMemory = item.isInMemory();
//	       long sizeInBytes = item.getSize();
	       str=System.currentTimeMillis()+".png";   //重命名图片
         //获取项目图片保存路径
	String path=request.getSession().getServletContext().getRealPath("img")+"\\"+str;
		File file=new File(path);
                str="img/"+str; //重命名保存到数据库名图
                try {
                   item.write(file); //将上传图片获取并写入项目保存图片的文件中			         } catch (Exception e) {	
 			e.printStackTrace();
                 }	
	   }
        }					                                          

     Android上传图片:

 

      if(id==R.id.btn_upload_img){  //点击上传图片按钮上传图片
			Intent intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			 startActivityForResult(intent, RESULT_LOAD_IMAGE);
		}
	}
	//获取图片路径
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		  super.onActivityResult(requestCode, resultCode, data); 
		 if(resultCode== getActivity().RESULT_OK) {
		    switch (requestCode) {
		      case RESULT_LOAD_IMAGE:
			Uri selectedImage = data.getData();
    			String[] filePathColumn = { MediaStore.Images.Media.DATA };
    			Cursor cursor =  getActivity().getContentResolver().query(selectedImage,
    					filePathColumn, null, null, null);
    			cursor.moveToFirst();
    			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    			picturePath = cursor.getString(columnIndex);   //保存图片路径    		
    			cursor.close();
                     break;  
		     default:
			break;
		  }			  
             }
	}

         File file = new File(picturePath);  //通过图片路径获取文件,通过http请求将文件传到服务器(解析获取图片同上Web)

    注:需要两个jar包,已上传

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics