前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用RESTful 创建文件接收 API

使用RESTful 创建文件接收 API

作者头像
keinYe
发布2019-12-19 17:00:25
1.9K0
发布2019-12-19 17:00:25
举报
文章被收录于专栏:keinYekeinYe

文件「文本、文档、图片等等」是一个服务器不可缺少的部分,在 使用 Flask 创建 RESTful 服务 介绍了如何使用 Flask 创建一个支持 RESTful API 的服务器。这篇文章介绍如何使用 RESTful API 来完成文件的接收,并将文件保存在静态目录下。

以下是文件接收的代码「这是实现的是图片的接收」:

代码语言:javascript
复制
    parse = reqparse.RequestParser()    parse.add_argument('image', type=werkzeug.datastructures.FileStorage, location='files')    args = parse.parse_args()    stream = args.get('image')    basepath = current_app.config['BASEDIR']    upload_path = os.path.join(basepath, "server/static/uploads", secure_filename(stream.filename))    stream.save(upload_path)

The FileStorage class is a thin wrapper over incoming files. It is used by the request object to represent uploaded files. All the attributes of the wrapper stream are proxied by the file storage so it’s possible to do storage.read() instead of the long form storage.stream.read().

以上代码实现通过参数传输图片上传至服务端,在服务端以文件流的方式读取文件并将文件保存到服务器的静态文件目录下。

以下是通过 Postman 测试文件上传 API 的配置方式。

在 Anddroid 下是使用 Retrofit 来完成文件的上传示例代码如下:

代码语言:javascript
复制
public class Server {    private static final String TAG = "Server";
    public interface UserAPIs {        @Multipart        @POST("/image")        Call<ResponseBody> uploadImage(@Part MultipartBody.Part file,                                       @Part("name") RequestBody requestBody);    }
    public static void uploadImageToServer(Context context, String filePath, Callback callback) {        Retrofit retrofit = NetworkClinet.getRetrofitClinet(context);        UserAPIs uploadAPIs = retrofit.create(UserAPIs.class);        //Create a file object using file path        File file = new File(filePath);        // Create a request body with file and image media type        RequestBody fileReqBody = RequestBody.create(file, MediaType.parse("image/*"));        // Create MultipartBody.Part using file request-body,file name and part name        MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), fileReqBody);        //Create request body with text description and text media type        RequestBody description = RequestBody.create("image-type", MediaType.parse("text/plain"));
        Call call = uploadAPIs.uploadImage(part, description);        call.enqueue(callback);    }}
final class NetworkClinet {    private static final String BASE_URL = "http://127.0.0.1:5000/";    private static Retrofit retrofit;
    public static Retrofit getRetrofitClinet(Context context) {        if (retrofit == null) {            OkHttpClient okHttpClient = new OkHttpClient.Builder()                    .connectTimeout(30, TimeUnit.SECONDS)                    .writeTimeout(30, TimeUnit.SECONDS)                    .readTimeout(30, TimeUnit.SECONDS)                    .build();            retrofit = new Retrofit.Builder()                    .baseUrl(BASE_URL)                    .client(okHttpClient)                    .addConverterFactory(GsonConverterFactory.create())                    .build();        }        return retrofit;    }}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 keinYe 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Serverless HTTP 服务
Serverless HTTP 服务基于腾讯云 API 网关 和 Web Cloud Function(以下简称“Web Function”)建站云函数(云函数的一种类型)的产品能力,可以支持各种类型的 HTTP 服务开发,实现了 Serverless 与 Web 服务最优雅的结合。用户可以快速构建 Web 原生框架,把本地的 Express、Koa、Nextjs、Nuxtjs 等框架项目快速迁移到云端,同时也支持 Wordpress、Discuz Q 等现有应用模版一键快速创建。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档