php okhttp3 上传文件,Android使用OKHttp库实现视频文件的上传到服务器功能

本文介绍了如何在Android端使用OkHttp库将视频文件上传到Flask服务器。首先展示了服务器接口的Python代码,然后详细讲解了Android端的实现,包括XML布局、Activity类和Okhttp网络通信类的代码。通过监听进度并使用ProgressRequestBody实现文件上传的进度显示。
摘要由CSDN通过智能技术生成

1 服务器接口简介

此处我使用的服务器接口是使用Flask编写,具体实现代码:

# -*- coding: utf-8 -*-

from flask import Flask, render_template, jsonify, request

import time

import os

import base64

app = Flask(__name__)

UPLOAD_FOLDER = 'E:\myupload\picture'

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

basedir = os.path.abspath(os.path.dirname(__file__))

ALLOWED_EXTENSIONS = set(['txt', 'png', 'jpg', 'xls', 'JPG', 'PNG', 'xlsx', 'gif', 'GIF','mp4'])

# 用于判断文件后缀

def allowed_file(filename):

return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

# 上传文件

@app.route('/api/upload', methods=['POST'], strict_slashes=False)

def api_upload():

file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])

if not os.path.exists(file_dir):

os.makedirs(file_dir)

f = request.files['myfile'] # 从表单的file字段获取文件,myfile为该表单的name值

if f and allowed_file(f.filename): # 判断是否是允许上传的文件类型

fname = f.filename

print fname

ext = fname.rsplit('.', 1)[1] # 获取文件后缀

unix_time = int(time.time())

new_filename = str(unix_time) + '.' + ext # 修改了上传的文件名

f.save(os.path.join(file_dir, new_filename)) # 保存文件到upload目录

print new_filename

token = base64.b64encode(new_filename)

print token

return jsonify({"errno": 0, "errmsg": "上传成功", "token": token})

else:

return jsonify({"errno": 1001, "errmsg": "上传失败"})

if __name__ == '__main__':

app.run(debug=True)

2 Android端代码实现

代码分三部分:

分别是xml布局文件,Activity类,和Okhttp网络通信类。

2.1 xml布局文件

activity_video_upload.xml:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_height="60dp"

android:layout_marginTop="80dp"

android:orientation="horizontal">

android:layout_width="match_parent"

android:layout_height="50dp"

android:layout_gravity="center_vertical"

android:gravity="center"

android:text="视频名称:"

android:textColor="@color/black2"

android:textSize="18dp"/>

android:id="@+id/upload_video_name"

android:layout_width="280dp"

android:layout_height="50dp"

android:layout_gravity="center_vertical"

android:hint="请输入上传视频名称"

android:layout_marginLeft="5dp"

android:textSize="18dp"

/>

android:id="@+id/video_select"

android:layout_width="match_parent"

android:layout_height="44dp"

android:layout_marginLeft="100dp"

android:layout_marginRight="100dp"

android:layout_marginTop="80dp"

android:background="@drawable/exit_btn_blue"

android:text="选择视频"

android:textStyle="bold"

android:textColor="@android:color/white"

android:textSize="20sp"/>

android:id="@+id/video_upload"

android:layout_width="ma

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值