• 冲刺(九)


    徐利峰:

     

     

     

    昨天的bug今天已经完成,原因是布局的问题,导致评论数据部分没有显示出来。解决的办法是:用滑动布局将照片和评论数据包裹起来,之后设置总体的布局与底部评论栏间隔开。这样就不会使评论框挡住评论的数据。

    今天的完成:实现收藏,点赞,关注功能。

    遇到的困难有,在使用handler+Thread,子线程不能直接修改UI,需要创建handler对象,然后在子线程中发消息,然后在handler中捕获所需消息,实现响应。学会这个后,接下来就好设计收藏,点赞,关注这些功能,同时在逻辑思考上要设置图标的颜色变化,在这里我学习了一个“如何判断当前的资源文件是哪一个”,第二个比较繁琐的地方就是“做web服务器的访问数据请求”,比较容易。

    不得不说这个handler+Thread线程真棒

    明天的任务:实现全局搜索(含搜索记录)

    代码:Android

    复制代码
    package com.example.newbsh.UI.home.hometype.news.shownews;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.text.method.ScrollingMovementMethod;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.request.RequestOptions;
    import com.example.newbsh.HttpUtil.HttpUtil;
    import com.example.newbsh.MainActivity;
    import com.example.newbsh.R;
    import com.example.newbsh.UI.home.hometype.news.shownews.comment.BlogComment;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.lang.ref.WeakReference;
    import java.util.Objects;
    
    public class ShowNewsActivity extends AppCompatActivity implements View.OnClickListener {
        TextView textViewtitle,textViewdoc,textViewdate,textViewbkauthorname;
        LinearLayout piclinearLayout,commentLayout;
        TextView msg;
        Button send;
        private ImageView support,collect,bkphoto;
        private Button attention;
        private String title,authorname;
        private String username= MainActivity.getUserName();
        private ShowBlogHandler blogHandler;
        String url;
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_news);
            blogHandler = new ShowBlogHandler(this);
            init();//初始化文章的内容
            isAttention();//判断是否关注
            isCollect();//判断是否收藏
            isSupport();//判断是否点赞
            showComment();//展示评论
        }
    
    
    
        private void init() {
            textViewtitle = findViewById(R.id.NewsTitle);
            textViewdoc = findViewById(R.id.Newsdoc);
            textViewdate = findViewById(R.id.Newsdate);
            piclinearLayout=findViewById(R.id.picturelinearlayout);
            commentLayout=findViewById(R.id.comment_layout);
            textViewdoc.setMovementMethod(new ScrollingMovementMethod());
            support=findViewById(R.id.support);
            collect=findViewById(R.id.collect);
            attention=findViewById(R.id.attention);
            msg=findViewById(R.id.msg);
            send=findViewById(R.id.send);
            bkphoto=findViewById(R.id.userphoto);
            textViewbkauthorname=findViewById(R.id.bkname);
            initdoc();
            support.setOnClickListener(this);
            collect.setOnClickListener(this);
            attention.setOnClickListener(this);
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String comment=msg.getText().toString();
                    if(comment.isEmpty()){
                        Toast.makeText(ShowNewsActivity.this, "发送内容不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    writeComment(comment);      //开启线程
                }
            });
    
        }
    
    
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.attention:
                    attentionAction();
                    break;
                case R.id.collect:
                    collectAction();
                    break;
                case R.id.support:
                    supportAction();
                    break;
                default:
                    break;
            }
        }
        private void isSupport() {    //是否点赞
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("data", "点赞");
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/findsupport?username=" + username + "&title=" + title + "";
                    boolean isthumb = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 300;
                    msg.obj = isthumb;
                    blogHandler.sendMessage(msg);
                    Log.d("点赞", String.valueOf(isthumb));
                }
            }).start();
        }
    
        private void isCollect() {    //是否收藏
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("data", "收藏");
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/findcollect?username=" + username + "&title=" + title + "";
                    boolean isStore = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 400;
                    msg.obj = isStore;
                    blogHandler.sendMessage(msg);
                    Log.d("收藏", String.valueOf(isStore));
                }
            }).start();
        }
    
        private void isAttention() {   //是否关注
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("data", "关注");
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/findattention?username=" + username + "&authorname=" + authorname + "";
                    boolean isAttention = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 500;
                    msg.obj = isAttention;
                    blogHandler.sendMessage(msg);
                    Log.d("关注", String.valueOf(isAttention));
                }
            }).start();
        }
    
        private void attentionAction() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String text = attention.getText().toString();
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/attentionaction?username=" + username + "&authorname=" + authorname +
                            "&type=" + text.equals("关注") + "";
                    boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 700;
                    msg.obj = flag;
                    blogHandler.sendMessage(msg);
                }
            }).start();
        }
    
        private void collectAction() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/collectaction?username=" + username + "&title=" + title + "&" +
                            "type=" + judegIsDraw(collect) + "";
                    boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 800;
                    msg.obj = flag;
                    blogHandler.sendMessage(msg);
                }
            }).start();
        }
    
        private void supportAction() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String requrl = "http://39.97.181.86/BaiXiaoSheng/supportaction?username=" + username + "&title=" + title + "&" +
                            "type=" + judegIsDraw(support) + "";
                    boolean flag = Boolean.parseBoolean(String.valueOf(HttpUtil.sendPost(requrl, "")));
                    Message msg = new Message();
                    msg.what = 900;
                    msg.obj = flag;
                    blogHandler.sendMessage(msg);
                }
            }).start();
        }
    
        private boolean judegIsDraw(View view) {   //判断当前资源文件
            boolean flag=false;
            switch (view.getId()) {
                case R.id.collect:
                    flag= Objects.equals(((ImageView) view).getDrawable().getCurrent().getConstantState(),
                            getResources().getDrawable(R.drawable.ic_turned_in_not).getConstantState());
                    Log.d("collect",String.valueOf(flag));
                    break;
                case R.id.support:
                    flag= Objects.equals(((ImageView) view).getDrawable().getCurrent().getConstantState(),
                            getResources().getDrawable(R.drawable.ic_thumb_not_up).getConstantState());
                    Log.d("support",String.valueOf(flag));
                    break;
                default:
                    break;
            }
            return flag;
        }
    /*
    写评论调用的线程
     */
        private void writeComment(String comment)
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String reqdata="http://39.97.181.86/BaiXiaoSheng/sendnewscomment?username="+username+"&comment="+comment+"&title="+title;
                    String json=String.valueOf(HttpUtil.sendPost(reqdata,""));
                    Message msg=new Message();
                    msg.what=1000;
                    msg.obj=json;
                    blogHandler.sendMessage(msg);
                    Log.d("writecomment",json);
                }
            }).start();
        }
     /*
     初始化展示该博客的评论内容
      */
        private void showComment() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("data", "评论");
                    String requrl="http://39.97.181.86/BaiXiaoSheng/findallcomments?title="+title;
                    String json = String.valueOf(HttpUtil.sendPost(requrl, ""));
                    Message msg = new Message();
                    msg.what = 600;
                    msg.obj = json;
                    blogHandler.sendMessage(msg);
                    Log.d("评论", json);
                }
            }).start();
        }
    
    /*
    加载新闻文章
     */
        private void initdoc() {
            Bundle bundle = getIntent().getExtras();
            /*
            获取图片的链接
             */
            url=bundle.getString("url");
            String []urls=url.split(" ");
    
            /*
            设置文章的内容
             */
            if (bundle != null) {
                title=bundle.getString("title");
                textViewtitle.setText(bundle.getString("title"));
                textViewdoc.setText(bundle.getString("doc"));
                textViewdate.setText("文章发布时间为:"+bundle.getString("date"));
                textViewbkauthorname.setText(bundle.getString("authorname"));
                authorname=bundle.getString("authorname");
                Glide.with(this)
                        .load(bundle.getString("authorurl"))
                        .circleCrop()
                        .into(bkphoto);
            } else {
                Log.i("bundle", "为空。。。。。。");
            }
            /*
            动态加载图片
             */
    
            RequestOptions options = new RequestOptions()
                    .error(R.drawable.error)
                    .placeholder(R.drawable.loading);
            for(int i=0;i<urls.length;i++)
            {
                ImageView imageView=new ImageView(this);
                Glide.with(this)
                        .load(urls[i])
                        .apply(options)
                        .into(imageView);
                LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layout.setMargins(0, 10, 0, 0);
                imageView.setLayoutParams(layout);
                piclinearLayout.addView(imageView);
            }
        }
    
    
        static class ShowBlogHandler extends Handler {     //防止内存泄漏
            private final ShowNewsActivity mcontext;
    
            ShowBlogHandler(ShowNewsActivity activity) {
                WeakReference<ShowNewsActivity> weakReference = new WeakReference<>(activity);
                mcontext = weakReference.get();
            }
    
            @SuppressLint("SetTextI18n")
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
    
                switch (msg.what) {
                    case 300:
                        if (Boolean.parseBoolean(msg.obj.toString()))
                            mcontext.support.setImageResource(R.drawable.ic_thumb_up);
                        break;
                    case 400:
                        if (Boolean.parseBoolean(msg.obj.toString()))
                            mcontext.collect.setImageResource(R.drawable.ic_turned_in);
                        break;
                    case 500:
                        if (Boolean.parseBoolean(msg.obj.toString()))
                            mcontext.attention.setText("已关注");
                        break;
                    case 600:
                        try {
                            JSONArray comments = new JSONArray(msg.obj.toString());
                            Log.i("length:", ""+comments.length());
                            for (int i = 0; i < comments.length(); i++) {
                                JSONObject object = comments.getJSONObject(i);
                                BlogComment blogComment = new BlogComment(mcontext);
                                if (!object.isNull("imgurl")) {
                                    Glide.with(mcontext)
                                            .load(object.getString("imgurl"))
                                            .circleCrop()
                                            .into(blogComment.getImageView());
                                }
                                blogComment.getName().setText(object.getString("username"));
                                blogComment.getComment().setText(object.getString("comment"));
                                blogComment.getDate().setText(object.getString("date"));
                                Log.i("次数:", ""+object.getString("comment"));
                                mcontext.commentLayout.addView(blogComment,0);
    
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 700:
                        if (Boolean.parseBoolean(msg.obj.toString())) {
                            String text = mcontext.attention.getText().toString();
                            if (text.equals("关注")) {
                                mcontext.attention.setText("已关注");
                                Toast toast = Toast.makeText(mcontext, "关注成功", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            } else {
                                mcontext.attention.setText("关注");
                                Toast toast = Toast.makeText(mcontext, "已取消关注", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            }
                        } else {
                            Toast toast = Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        }
                        break;
                    case 800:
                        if (Boolean.parseBoolean(msg.obj.toString())) {
                            if (mcontext.judegIsDraw(mcontext.collect)) {
                                mcontext.collect.setImageResource(R.drawable.ic_turned_in);
                                Toast toast = Toast.makeText(mcontext, "收藏成功", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            } else {
                                mcontext.collect.setImageResource(R.drawable.ic_turned_in_not);
                                Toast toast = Toast.makeText(mcontext, "已取消收藏", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            }
                        } else {
                            Toast toast = Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        }
                        break;
                    case 900:
                        if (Boolean.parseBoolean(msg.obj.toString())) {
                            if (mcontext.judegIsDraw(mcontext.support)) {
                                mcontext.support.setImageResource(R.drawable.ic_thumb_up);
                                Toast toast = Toast.makeText(mcontext, "点赞成功", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            } else {
                                mcontext.support.setImageResource(R.drawable.ic_thumb_not_up);
                                Toast toast = Toast.makeText(mcontext, "已取消点赞", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                            }
                        } else {
                            Toast toast = Toast.makeText(mcontext, "未知原因,操作失败", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        }
                        break;
                    case 1000:
                        try {
                            JSONObject object=new JSONObject(msg.obj.toString());
                                BlogComment blogComment=new BlogComment(mcontext);
                                blogComment.getDate().setText(object.getString("date"));
                                blogComment.getComment().setText(object.getString("comment"));
                                blogComment.getName().setText(object.getString("username"));
                                if(!object.isNull("imgurl")){
                                    if(object.getString("imgurl").length()>0)
                                        Glide.with(mcontext)
                                                .load(object.getString("imgurl"))
                                                .circleCrop()
                                                .into(blogComment.getImageView());
                                }
                                mcontext.commentLayout.addView(blogComment,0);
                                mcontext.msg.setText("");
                                mcontext.msg.clearFocus();
                                InputMethodManager imm= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE);   //键盘收缩
                                if (imm != null) {
                                    imm.hideSoftInputFromWindow(mcontext.msg.getWindowToken(),0);
                                }
                                Toast toast = Toast.makeText(mcontext, "评论成功", Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 0);
                                toast.show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    
    }
    复制代码

    李浩:实现写博客功能,可以上传图片。

    UI界面设计:

         

    由于博客发布可能附加图片,但是图片(或者任何文件)信息必须放在http请求体的正文之中,这就需要我们使用HttpUrlConnection的时候构建Http正文。

    我们先来看一下Http正文格式:

    复制代码
     1 POST /api/feed/ HTTP/1.1  
     2 Accept-Encoding: gzip  
     3 Content-Length: 225873  
     4 Content-Type: multipart/form-data; boundary=OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
     5 Host: www.myhost.com  
     6 Connection: Keep-Alive  
     7   
     8 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
     9 Content-Disposition: form-data; name="lng"  
    10 Content-Type: text/plain; charset=UTF-8  
    11 Content-Transfer-Encoding: 8bit  
    12   
    13 116.361545  
    14 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
    15 Content-Disposition: form-data; name="lat"  
    16 Content-Type: text/plain; charset=UTF-8  
    17 Content-Transfer-Encoding: 8bit  
    18   
    19 39.979006  
    20 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
    21 Content-Disposition: form-data; name="images"; filename="/storage/emulated/0/Camera/jdimage/1xh0e3yyfmpr2e35tdowbavrx.jpg"  
    22 Content-Type: application/octet-stream  
    23 Content-Transfer-Encoding: binary  
    24   
    25 这里是图片的二进制数据  
    26 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp--  
    复制代码

    格式分析

    请求头分析

    我们先看报文格式中的第一行:
    POST /api/feed/ HTTP/1.1 

    这一行就说明了这个请求的请求方式,即为POST方式,要请求的子路径为/api/feed/,例如我们的服务器地址为www.myhost.com,然后我们的这个请求的完整路径就是www.myhost.com/api/feed/,最后说明了HTTP协议的版本号为1.1。

    Accept-Encoding: gzip  
    Content-Length: 225873  
    Content-Type: multipart/form-data; boundary=OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
    Host: www.myhost.com  
    Connection: Keep-Alive  
    这几个header的意思分别为服务器返回的数据需要使用gzip压缩、请求的内容长度为225873、内容的类型为"multipart/form-data"、请求参数分隔符(boundary)为OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp、请求的根域名为www.myhost.com、HTTP连接方式为持久连接( Keep-Alive)。
     
    其中这里需要注意的一点是分隔符,即boundary。boundary用于作为请求参数之间的界限标识,例如参数1和参数2之间需要有一个明确的界限,这样服务器才能正确的解析到参数1和参数2。但是分隔符并不仅仅是boundary,而是下面这样的格式:-- + boundary。例如这里的boundary为OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp,那么参数分隔符则为:
    --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp 
    不管boundary本身有没有这个"--",这个"--"都是不能省略的。

    我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成之后立即断开连接(HTTP协议为无连接的协议);当使用Keep-Alive模式(又称持久连接、连接重用)时,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后续请求时,Keep-Alive功能避免了建立或者重新建立连接。

    请求实体分析

    请求实体其实就是HTTP POST请求的参数列表,每个参数以请求分隔符开始,即-- + boundary。例如下面这个参数。
    复制代码
    1 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
    2 Content-Disposition: form-data; name="lng"  
    3 Content-Type: text/plain; charset=UTF-8  
    4 Content-Transfer-Encoding: 8bit  
    5   
    6 116.361545 
    复制代码
    上面第一行为--OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp,也就是--加上boundary内容,最后加上一个换行 (这个换行不能省略),换行的字符串表示为" "。第二行为Content-Disposition和参数名,这里的参数名为lng,即经度。Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名,这里我们不过多关注。第三行为Content-Type,即WEB 服务器告诉浏览器自己响应的对象的类型,还有指定字符编码为UTF-8。第四行是描述的是消息请求(request)和响应(response)所附带的实体对象(entity)的传输形式,简单文本数据我们设置为8bit,文件参数我们设置为binary就行。然后添加两个换行之后才是参数的具体内容。例如这里的参数内容为116.361545。
    注意这里的每行之间都是使用“ ”来换行的,最后一行和参数内容之间是两个换行。文件参数也是一样的格式,只是文件参数的内容是字节流。
    这里要注意一下,普通文本参数和文件参数有如下两个地方的不同,因为其内容本身的格式是不一样的。
    普通参数:
    1 Content-Type: text/plain; charset=UTF-8  
    2 Content-Transfer-Encoding: 8bit  

    文件参数:

    1 Content-Type: application/octet-stream  
    2 Content-Transfer-Encoding: binary  

    参数实体的最后一行是: --加上boundary加上--,最后换行,这里的 格式即为: --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp--。

    具体代码如下:

    复制代码
     1 public static void sendPostImg(String actionUrl, Map<String, File> files) throws IOException {
     2 
     3         String BOUNDARY = java.util.UUID.randomUUID().toString();   //利用系统工具类生成界限符
     4         String PREFIX = "--", LINEND = "
    ";
     5         String MULTIPART_FROM_DATA = "multipart/form-data";
     6         String CHARSET = "UTF-8";
     7 
     8         URL uri = new URL(actionUrl);
     9         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
    10         conn.setReadTimeout(5 * 1000); // 缓存的最长时间
    11         conn.setDoInput(true);// 允许输入
    12         conn.setDoOutput(true);// 允许输出
    13         conn.setUseCaches(false); // 不允许使用缓存
    14         conn.setRequestMethod("POST");
    15         conn.setRequestProperty("connection", "keep-alive");
    16         conn.setRequestProperty("Charsert", "UTF-8");
    17         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
    18 
    19 //        // 首先组拼文本类型的参数
    20 //        StringBuilder sb = new StringBuilder();
    21 //        for (Map.Entry<String, String> entry : params.entrySet())
    22 //        {
    23 //            sb.append(PREFIX);
    24 //            sb.append(BOUNDARY);
    25 //            sb.append(LINEND);
    26 //            sb.append("Content-Disposition: form-data; name="" + entry.getKey() + """ + LINEND);
    27 //            sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
    28 //            sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
    29 //            sb.append(LINEND);
    30 //            sb.append(entry.getValue());
    31 //            sb.append(LINEND);
    32 //        }
    33 
    34         DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
    35 //        outStream.write(sb.toString().getBytes());
    36         InputStream in = null;
    37         // 发送文件数据
    38         if (files != null)
    39         {
    40             for (Map.Entry<String, File> file : files.entrySet())
    41             {
    42                 StringBuilder sb1 = new StringBuilder();
    43                 sb1.append(PREFIX);
    44                 sb1.append(BOUNDARY);
    45                 sb1.append(LINEND);
    46                 // name是post中传参的键 filename是文件的名称
    47                 sb1.append("Content-Disposition: form-data; name="file"; filename="" + file.getValue().getName() + """ + LINEND);
    48                 sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
    49                 sb1.append("Content-Transfer-Encoding: binary"+LINEND);
    50                 sb1.append(LINEND);
    51                 outStream.write(sb1.toString().getBytes());
    52                 Log.d("file",sb1.toString());
    53                 InputStream is = new FileInputStream(file.getValue());
    54                 byte[] buffer = new byte[1024];
    55                 int len = 0;
    56                 while ((len = is.read(buffer)) != -1)
    57                 {
    58                     outStream.write(buffer, 0, len);
    59                 }
    60 
    61                 is.close();
    62                 outStream.write(LINEND.getBytes());
    63             }
    64 
    65             // 请求结束标志
    66             byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
    67             outStream.write(end_data);
    68             outStream.flush();
    69             // 得到响应码
    70             int res = conn.getResponseCode();
    71             if (res == 200)
    72             {
    73                 in = conn.getInputStream();
    74                 int ch;
    75                 StringBuilder sb2 = new StringBuilder();
    76                 while ((ch = in.read()) != -1)
    77                 {
    78                     sb2.append((char) ch);
    79                 }
    80                 Log.d(TAG,"状态码:"+res);
    81             }else{
    82                 Log.d(TAG,"状态码:"+res);
    83             }
    84             outStream.close();
    85             conn.disconnect();
    86         }
    87         // return in.toString();
    88     }
    复制代码

    代码是我参考其他博客改的,按照我自己的需求,将一些参数附加到URL后传输,Http正文只传输文件,因为我的参数决定了文件的命名格式,这样后台不用遍历查找参数后在遍历一次寻找文件了,当然,如果你的参数比较重要的话还是将参数写到Http正文中,这样较为安全。

    后台文件接收代码:

    复制代码
     1 String temppath="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/tempfile";
     2         String path="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/webapps/STDEverything/images/blog";
     3         
     4         String userid=request.getParameter("userid");
     5         String blogid=request.getParameter("blogid");
     6         
     7         DiskFileItemFactory disk = new DiskFileItemFactory(1024*10,new File(temppath));
     8         ServletFileUpload up = new ServletFileUpload(disk);
     9         List<FileItem> list;
    10         try {
    11             list=up.parseRequest(request);
    12             for(FileItem item:list) {
    13                 if(!item.isFormField()) {
    14                     InputStream inputStream=item.getInputStream();
    15                     String filename=item.getName();
    16                     String imgname=userid+"_"+DBUtil.getIdentifier()+"_"+filename;
    17                     OutputStream outputStream=new FileOutputStream(path+"/"+imgname);
    18                     System.out.println(imgname);
    19                     int len=0;
    20                     byte buff[]=new byte[1024];
    21                     while((len=inputStream.read(buff))!=-1) {
    22                         outputStream.write(buff,0,len);
    23                     }
    24                     outputStream.flush();
    25                     outputStream.close();
    26                     inputStream.close();
    27                     DBUtil.writeBlogImg(request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/"))
    28                             +"/images/blog/"+imgname, blogid);
    29                 }else {
    30                     System.out.println(item.getFieldName());
    31                 }
    32             }
    33         } catch (FileUploadException e) {
    34             // TODO Auto-generated catch block
    35             e.printStackTrace();
    36             response.getWriter().write("no");
    37         }
    38         response.getWriter().write("yes");
    复制代码

    要想运行上文中的代码接收文件,需要导入两个jar包,

    链接:http://pan.baidu.com/s/1jIbyn5s 密码:84se

    关于后台接收文件的学习可以参考这篇博客:

    https://blog.csdn.net/linghuainian/article/details/82253247?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3

    值得一提的是,当传输文件时,你的Http正文编码格式必须改为"multipart/form-data",然而改为这种格式之后,普通的request.getParameter方式就无法获取参数了,但可以获取fileitem.InputStream来进行转换,这也就是上文中说的为什么要遍历获取参数的原因。

    至于Android项目的后期设想,因为博客图片是和博客id进行绑定的,而且我存进去的是该图片的网址,当后期显示博文的具体内容时,可以先请求下来博客网址信息,用Android的Glide插件异步加载图片。

  • 相关阅读:
    局部变量、结构体和main函数
    类、对象和方法
    Archiver 浅析
    NSArry的常见方法
    iOS 键盘隐藏
    //获取拼音首字母
    UI通过UISlider编写游戏第六感
    UI中各种手势的使用点击,捏合,清扫,旋转,平移,边缘移动,长按
    IOS学习笔记 -- Modal和Quartz2D
    多控制器管理练习 -- 私人通讯录
  • 原文地址:https://www.cnblogs.com/xlk3/p/12793859.html
Copyright © 2020-2023  润新知