zoukankan      html  css  js  c++  java
  • android 上传图片 .html 和android 客户端

    1.先用html的原始标签来完成上传图片功能,后台使用asp.net.

    2.再用android的okhttp来代替html。

    3.图片格式固定为png。支持多种,稍微修改下代码。测试通过。

    html:upload/aa.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>HTML控件配合ASP。NET 上传</title>
    </head>
    <body>
    <form name="uploadForm" method="post" enctype="multipart/form-data" action="/upload/Default.aspx">
    <input id="name" name="name" style="220px;" />
    <input type="file" id="imgFile" name="imgFile" style="220px;" />
    <input type="file" id="imgFile2" name="imgFile2" style="220px;" />
    <input type="file" id="imgFile3" name="imgFile3" style="220px;" />
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>

    asp.net 的后端:upload/Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="upload._Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace upload
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    string name=this.Request.Form.Get("name");
                    string path = Server.MapPath("");
                    this.Label1.Text = "name:" + name + ".path:" + path;
    
                    if (this.Request.Files.Count > 0)
                    {
                        for (int i = 0; i < this.Request.Files.Count;i++ )
                        {
                            HttpPostedFile file = this.Request.Files.Get(i);
                            string pathstr = path + "/aaupload"+System.DateTime.Now.ToFileTime().ToString() +"index"+  i.ToString()+ ".png";
                            try
                            {
                                file.SaveAs(pathstr);
                            }
                            catch
                            {
                                this.Label1.Text ="error:"+pathstr;
                            }
                        }
                    }
                }
            }
        }
    }

    android 客户端: xxapk

    private void myUploadPic()
        {
            //1.检测到未上传的图片。2.利用okhttp的http协议上传图片。
    
            SharedPreferences share= getSharedPreferences(SHARE_FILENAME, MODE_PRIVATE);
            String strpic1= share.getString(SHARE_FILE1,"" );
            final File file=new File(strpic1);
    
            Thread thread=new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    if(file!=null && file.exists())
                    {
                        LSOKHttp.postFile("http://192.168.0.106/upload/Default.aspx", null, file);
                    }
                }
            });
            thread.start();
        }
    public static void postFile(String url,  Callback callbackHandler, File file)
        {
            //FormBody.Builder formbody=new FormBody.Builder();
            // .post(formbody.build())
            MultipartBody.Builder multipartBodyBulider=new MultipartBody.Builder();
            multipartBodyBulider.setType(MultipartBody.FORM);
            RequestBody requestBody=RequestBody.create(MediaType.parse("image/png"), file);
            multipartBodyBulider.addFormDataPart("file",file.getName(),requestBody);
    
            MultipartBody multipartBody = multipartBodyBulider.build();
    
            Request request  = new Request.Builder()
                    .url(url)
                    .post(multipartBody)
                    .build();
            try
            {
                client.newCall(request).execute();
            } catch (Exception e)
            {
                LSLog.Log_Exception(e);
            }
        }
  • 相关阅读:
    3. applicationCache
    9. 水平垂直居中的方法
    2. 贝塞尔曲线bezierCurveTo
    相识python 之小数据池 集合
    相识python --------- 列表 元祖 range 范围
    相识python while循环 代码块 编码初识 运算符
    相识python第二步:变量 注释 str int bool 用户交换 流程控制语句的解释用法
    python学习基础知识
    python的基础知识
    我对python的见解
  • 原文地址:https://www.cnblogs.com/lsfv/p/12193171.html
Copyright © 2011-2022 走看看