标签:python 成功 xtend str button onclick tpc sed gradle
继上篇成功的与服务器端通信上之后,我现在需要将安卓本地的图片上传到服务端。服务端接收图片存下来。
参考:https://blog.csdn.net/qq_26906345/article/details/91045074
Android客户端:
点击按钮会将文件上传到服务器,图片的地址我暂时是写死的。
服务器端:
接收到图片存储到写好的位置
相关代码如下:
Android客户端:由于布局文件只有一个按钮,故不在此展出。
MainActivity.java:主要就是设置了对按钮的监听。
package com.example.vesper.uploadpic; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import java.io.File; public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File f=new File(Environment.getExternalStorageDirectory().getPath()+"/mahc/image/1573125250817.jpg"); try { ImageUpload.run(f); } catch (Exception e) { e.printStackTrace(); } } }); } }
ImageUpload.java:开启线程上传图片。
1 package com.example.vesper.uploadpic; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import okhttp3.MediaType; 7 import okhttp3.MultipartBody; 8 import okhttp3.OkHttpClient; 9 import okhttp3.Request; 10 import okhttp3.RequestBody; 11 import okhttp3.Response; 12 13 public class ImageUpload{ 14 15 private static final MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg"); 16 private static final OkHttpClient client = new OkHttpClient(); 17 public static void run(File f) throws Exception { 18 final File file=f; 19 new Thread() { 20 @Override 21 public void run() { 22 //子线程需要做的工作 23 RequestBody requestBody = new MultipartBody.Builder() 24 .setType(MultipartBody.FORM) 25 .addFormDataPart("file","1.jpg", 26 RequestBody.create(MEDIA_TYPE_JPG, file)) 27 .build(); 28 //设置为自己的ip地址 29 Request request = new Request.Builder() 30 .url("http://192.168.xxx.xxx:5000/upload") 31 .post(requestBody) 32 .build(); 33 try(Response response = client.newCall(request).execute()){ 34 if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); 35 36 System.out.println(response.body().string()); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 }.start(); 42 } 43 }
Python服务器端:
uploadserver.py:
1 #-*-coding:utf-8-*- 2 from flask import Flask 3 from flask import request 4 import cv2 5 import os 6 from werkzeug.utils import secure_filename 7 app = Flask(__name__) 8 basedir=os.path.abspath(os.path.dirname(__file__)) 9 10 @app.route(‘/‘) 11 def test(): 12 return ‘服务器正常运行‘ 13 14 #此方法接收图片 15 @app.route(‘/upload‘,methods=[‘POST‘]) 16 def upload(): 17 f = request.files[‘file‘] 18 print(‘连接成功‘) 19 # 当前文件所在路径 20 basepath = os.path.dirname(__file__) 21 upload_path = os.path.join(basepath, ‘1115‘, secure_filename(f.filename)) 22 # 保存文件 23 f.save(upload_path) 24 return ‘保存成功‘ 25 26 if __name__ == ‘__main__‘: 27 app.run(host=‘0.0.0.0‘)
遇到的问题:
1.读文件权限要在Manifest.xml中加上:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2.在gradle中修改最小SDK为19.
标签:python 成功 xtend str button onclick tpc sed gradle
原文地址:https://www.cnblogs.com/glam/p/11883133.html