parent
							
								
									ff81d43e72
								
							
						
					
					
						commit
						21bad6b9c1
					
				| @ -0,0 +1,86 @@ | ||||
| /* | ||||
|  * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
 | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package com.arialyy.aria.core.common.http; | ||||
| 
 | ||||
| import android.text.TextUtils; | ||||
| import com.arialyy.aria.core.common.RequestEnum; | ||||
| import com.arialyy.aria.core.download.DownloadGroupTarget; | ||||
| import com.arialyy.aria.core.download.DownloadGroupTaskEntity; | ||||
| import com.arialyy.aria.core.download.DownloadTaskEntity; | ||||
| import com.arialyy.aria.core.inf.AbsTarget; | ||||
| import com.arialyy.aria.core.inf.IPostDelegate; | ||||
| import com.arialyy.aria.core.inf.ITarget; | ||||
| import com.arialyy.aria.util.ALog; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * post处理委托类 | ||||
|  */ | ||||
| public class PostDelegate<TARGET extends AbsTarget> implements IPostDelegate<TARGET>, ITarget { | ||||
|   private static final String TAG = "PostDelegate"; | ||||
|   private TARGET mTarget; | ||||
| 
 | ||||
|   public PostDelegate(TARGET target) { | ||||
|     mTarget = target; | ||||
|     mTarget.getTaskEntity().setRequestEnum(RequestEnum.POST); | ||||
|   } | ||||
| 
 | ||||
|   @Override public TARGET setParams(Map<String, String> params) { | ||||
|     mTarget.getTaskEntity().setParams(params); | ||||
|     if (mTarget instanceof DownloadGroupTarget) { | ||||
|       for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) { | ||||
|         subTask.setParams(params); | ||||
|       } | ||||
|     } | ||||
|     return mTarget; | ||||
|   } | ||||
| 
 | ||||
|   @Override public TARGET setParam(String key, String value) { | ||||
|     if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) { | ||||
|       ALog.d(TAG, "key 或value 为空"); | ||||
|       return mTarget; | ||||
|     } | ||||
|     Map<String, String> params = mTarget.getTaskEntity().getParams(); | ||||
|     if (params == null) { | ||||
|       params = new HashMap<>(); | ||||
|       mTarget.getTaskEntity().setParams(params); | ||||
|     } | ||||
|     params.put(key, value); | ||||
|     if (mTarget instanceof DownloadGroupTarget) { | ||||
|       for (DownloadTaskEntity subTask : ((DownloadGroupTaskEntity) mTarget.getTaskEntity()).getSubTaskEntities()) { | ||||
|         subTask.setParams(params); | ||||
|       } | ||||
|     } | ||||
|     return mTarget; | ||||
|   } | ||||
| 
 | ||||
|   @Override public void start() { | ||||
|     mTarget.start(); | ||||
|   } | ||||
| 
 | ||||
|   @Override public void stop() { | ||||
|     mTarget.stop(); | ||||
|   } | ||||
| 
 | ||||
|   @Override public void resume() { | ||||
|     mTarget.resume(); | ||||
|   } | ||||
| 
 | ||||
|   @Override public void cancel() { | ||||
|     mTarget.cancel(); | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,31 @@ | ||||
| /* | ||||
|  * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
 | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0
 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package com.arialyy.aria.core.inf; | ||||
| 
 | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * post 通用处理接口 | ||||
|  */ | ||||
| public interface IPostDelegate<TARGET extends ITarget> { | ||||
| 
 | ||||
|   /** | ||||
|    * 设置Post请求参数 | ||||
|    */ | ||||
|   TARGET setParams(Map<String, String> params); | ||||
| 
 | ||||
|   TARGET setParam(String key, String value); | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| # coding=utf-8 | ||||
| 
 | ||||
| import os | ||||
| from flask import Flask, send_from_directory, request | ||||
| 
 | ||||
| app = Flask(__name__) | ||||
| 
 | ||||
| 
 | ||||
| @app.route("/download/<path:filename>", methods=['POST', 'GET']) | ||||
| def downloader(filename): | ||||
|     """ | ||||
|     不支持断点的下载 | ||||
|     """ | ||||
|     data = request.values.get('key') | ||||
|     print data | ||||
|     dirpath = 'D:/test' | ||||
|     return send_from_directory(dirpath, filename, as_attachment=True)  # as_attachment=True 一定要写,不然会变成打开,而不是下载 | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
|     app.run(host='0.0.0.0', debug=True)  # 需要关闭防火墙 | ||||
| @ -0,0 +1,62 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
| import os | ||||
| from flask import Flask, request, url_for, send_from_directory | ||||
| from werkzeug import secure_filename | ||||
| 
 | ||||
| ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'rar', 'apk']) | ||||
| 
 | ||||
| app = Flask(__name__) | ||||
| app.config['UPLOAD_FOLDER'] = 'd:/db/' | ||||
| app.config['MAX_CONTENT_LENGTH'] = 1600 * 1024 * 1024 | ||||
| 
 | ||||
| """ | ||||
| 可以选择这个扩展 | ||||
| Flask-Uploads | ||||
| """ | ||||
| 
 | ||||
| html = ''' | ||||
|     <!DOCTYPE html> | ||||
|     <title>Upload File</title> | ||||
|     <h1>图片上传</h1> | ||||
|     <form method=post enctype=multipart/form-data> | ||||
|          <input type=file name=file> | ||||
|          <input type=submit value=上传> | ||||
|     </form> | ||||
|     ''' | ||||
| 
 | ||||
| 
 | ||||
| def allowed_file(filename): | ||||
|     return '.' in filename and \ | ||||
|            filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS | ||||
| 
 | ||||
| 
 | ||||
| @app.route('/uploads/<filename>') | ||||
| def uploaded_file(filename): | ||||
|     return send_from_directory(app.config['UPLOAD_FOLDER'], | ||||
|                                filename) | ||||
| 
 | ||||
| 
 | ||||
| @app.route('/', methods=['GET', 'POST']) | ||||
| def test(): | ||||
|     return'test' | ||||
| 
 | ||||
| 
 | ||||
| @app.route('/upload/', methods=['GET', 'POST']) | ||||
| def upload_file(): | ||||
|     print 'upload' | ||||
|     if request.method == 'POST': | ||||
|         print 'post' | ||||
|         file = request.files['file'] | ||||
|         print file | ||||
|         if file and allowed_file(file.filename): | ||||
|             print 'start save' | ||||
|             filename = secure_filename(file.filename) | ||||
|             file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | ||||
|             # file_url = url_for('uploaded_file', filename=filename) | ||||
|             # return html + '<br><img src=' + file_url + '>' | ||||
|             return '200' | ||||
|     return '405' | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == '__main__': | ||||
|     app.run(host='0.0.0.0', port=5000) | ||||
					Loading…
					
					
				
		Reference in new issue