将图像从Android上传到Python服务器

将图像从Android上传到Python服务器,android,python,Android,Python,我想知道在Python中使用什么库或框架来读取Android客户端发布的图像 所以基本上,Android客户端在手机中获取一个图像文件并发送它 通过POST请求发送到Python服务器。在Python中读取图像时,我读到的唯一内容是通过套接字 我想询问一些关于如何实现Python服务器端通过POST读取图像的链接或示例代码。我有一个基于Python flask的后端web应用程序,它有这个方法来接受图像 @app.route('/getNoteText',methods=['GET','POST

我想知道在Python中使用什么库或框架来读取Android客户端发布的图像

所以基本上,Android客户端在手机中获取一个图像文件并发送它 通过POST请求发送到Python服务器。在Python中读取图像时,我读到的唯一内容是通过套接字


我想询问一些关于如何实现Python服务器端通过POST读取图像的链接或示例代码。

我有一个基于Python flask的后端web应用程序,它有这个方法来接受图像

@app.route('/getNoteText',methods=['GET','POST'])
def GetNoteText():
    if request.method == 'POST':
        file = request.files['pic']
        filename = file.filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        processImage(filename)            
    else:
        return "Y U NO USE POST?"
我的android函数调用这个方法如下

  /**
     * Uploading the file to server
     */
    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

        private String uploadFile() {
            String responseString = null;
            Log.d("Log", "File path" + opFilePath);
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                        new AndroidMultiPartEntity.ProgressListener() {

                           @Override
                          public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });
                ExifInterface newIntef = new ExifInterface(opFilePath);
                newIntef.setAttribute(ExifInterface.TAG_ORIENTATION,String.valueOf(2));
                File file = new File(opFilePath);
                entity.addPart("pic", new FileBody(file));
                totalSize = entity.getContentLength();
                httppost.setEntity(entity);

                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();


                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // Server response
                    responseString = EntityUtils.toString(r_entity);
                    Log.d("Log", responseString);
                } else {
                    responseString = "Error occurred! Http Status Code: "
                            + statusCode + " -> " + response.getStatusLine().getReasonPhrase();
                    Log.d("Log", responseString);
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;    
        }
    }
您可以使用此
uploadFileToServer
将任何类型的文件发送到后台。您不需要仅限于图像


希望这有帮助

我有一个基于python flask的后端web应用程序,它有这个方法来接受图像

@app.route('/getNoteText',methods=['GET','POST'])
def GetNoteText():
    if request.method == 'POST':
        file = request.files['pic']
        filename = file.filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        processImage(filename)            
    else:
        return "Y U NO USE POST?"
我的android函数调用这个方法如下

  /**
     * Uploading the file to server
     */
    private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {
            return uploadFile();
        }

        private String uploadFile() {
            String responseString = null;
            Log.d("Log", "File path" + opFilePath);
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
            try {
                AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                        new AndroidMultiPartEntity.ProgressListener() {

                           @Override
                          public void transferred(long num) {
                                publishProgress((int) ((num / (float) totalSize) * 100));
                            }
                        });
                ExifInterface newIntef = new ExifInterface(opFilePath);
                newIntef.setAttribute(ExifInterface.TAG_ORIENTATION,String.valueOf(2));
                File file = new File(opFilePath);
                entity.addPart("pic", new FileBody(file));
                totalSize = entity.getContentLength();
                httppost.setEntity(entity);

                // Making server call
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity r_entity = response.getEntity();


                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // Server response
                    responseString = EntityUtils.toString(r_entity);
                    Log.d("Log", responseString);
                } else {
                    responseString = "Error occurred! Http Status Code: "
                            + statusCode + " -> " + response.getStatusLine().getReasonPhrase();
                    Log.d("Log", responseString);
                }

            } catch (ClientProtocolException e) {
                responseString = e.toString();
            } catch (IOException e) {
                responseString = e.toString();
            }

            return responseString;    
        }
    }
您可以使用此
uploadFileToServer
将任何类型的文件发送到后台。您不需要仅限于图像


希望这有帮助

我准备了一个教程来做这个。它构建了一个Android应用程序,将图像上传到使用Python在Flask中创建的服务器。本教程的标题为“

我准备了一个教程来实现这一点。它构建了一个Android应用程序,将图像上传到使用Python在Flask中创建的服务器。本教程的标题为“

你会尝试像PIL这样的python图像库吗(python图像库)…你会尝试像PIL这样的python图像库吗(python图像库)…我知道这是一篇非常古老的文章,但你也应该使用
secure\u filename
from
werkzeug.utils
像这样,
filename=secure\u filename(file.filename)
因为你永远不应该信任任何用户输入。我知道这是一篇非常古老的帖子,但你也应该使用
werkzeug.utils
中的
secure\u filename
像这样,
filename=secure\u filename(file.filename)
因为你永远不应该信任任何用户输入。