share_project/
├── share.py
└── templates/└── index.html
import os
from flask import Flask, render_template, request, send_from_directory, redirect, url_forapp = Flask(__name__)
UPLOAD_FOLDER = r'D:\FileStorage'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
if not os.path.exists(UPLOAD_FOLDER):os.makedirs(UPLOAD_FOLDER)@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'POST':if 'file' not in request.files:return redirect(request.url)file = request.files['file']if file.filename == '':return redirect(request.url)if file:filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)file.save(filename)return redirect(url_for('index'))files = [f for f in os.listdir(app.config['UPLOAD_FOLDER'])if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], f))]return render_template('index.html', files=files)@app.route('/download/<path:filename>')
def download(filename):safe_path = os.path.abspath(app.config['UPLOAD_FOLDER'])requested_path = os.path.abspath(os.path.join(app.config['UPLOAD_FOLDER'], filename))if not requested_path.startswith(safe_path):return "非法访问路径", 403return send_from_directory(app.config['UPLOAD_FOLDER'],filename,as_attachment=True)if __name__ == '__main__':app.run(host='0.0.0.0', port=5000, debug=True)
<!DOCTYPE html>
<html>
<head><title>Windows文件传输服务</title><style>body { font-family: Segoe UI, sans-serif; max-width: 800px; margin: 20px auto; }.container { padding: 20px; border: 1px solid #ddd; }ul { list-style-type: none; padding: 0; }li { padding: 8px; border-bottom: 1px solid #eee; }a { color: #0066cc; text-decoration: none; }a:hover { text-decoration: underline; }</style>
</head>
<body><div class="container"><h1>📁 文件传输服务</h1><h2>⬆ 上传文件</h2><form method="post" enctype="multipart/form-data"><input type="file" name="file" style="padding: 8px;"><input type="submit" value="上传" style="padding: 8px 20px; background: #0066cc; color: white; border: none; cursor: pointer;"></form><h2>📋 文件列表(存储路径:{{ UPLOAD_FOLDER }})</h2><ul>{% for file in files %}<li><span style="width: 70%; display: inline-block;">{{ file }}</span><a href="{{ url_for('download', filename=file) }}">⬇ 下载</a></li>{% endfor %}</ul></div>
</body>
</html>