无需安装任何工具,使用系统自带的curl命令即可操作文件传输服务
使用curl上传任意文件,获取分享码:
curl -X POST -F "file=@文件路径" http://localhost:8000/api/upload
# 上传图片 curl -X POST -F "file=@photo.jpg" http://localhost:8000/api/upload # 上传文档 curl -X POST -F "file=@document.pdf" http://localhost:8000/api/upload # 上传任意文件 curl -X POST -F "file=@/path/to/your/file.txt" http://localhost:8000/api/upload
{
"code": "AB12CD34",
"expires_at": "2024-01-01T12:15:00",
"download_url": "/api/download/AB12CD34"
}
直接分享文本内容,无需创建文件。提供多种简化方式:
curl -X POST --data "你的文本内容" http://localhost:8000/api/text
# 分享简单文本 ⭐ 最简单 curl -X POST --data "Hello World!" http://localhost:8000/api/text # 分享多行文本 curl -X POST --data "第一行 第二行 第三行" http://localhost:8000/api/text # 从文件分享 curl -X POST --data @myfile.txt http://localhost:8000/api/text # 通过管道分享 echo "Hello from pipeline!" | curl -X POST --data @- http://localhost:8000/api/text
curl -X POST -F "content=文本内容" -F "filename=文件名.txt" http://localhost:8000/api/share-text-form
# 指定文件名 curl -X POST -F "content=Hello World!" -F "filename=hello.txt" http://localhost:8000/api/share-text-form # 分享代码 curl -X POST -F "content=#!/bin/bash echo Hello" -F "filename=script.sh" http://localhost:8000/api/share-text-form
curl -X POST -H "Content-Type: application/json" \
-d '{"content":"文本内容","filename":"文件名.txt"}' \
http://localhost:8000/api/share-text
使用分享码下载文件到本地:
curl -O -J http://localhost:8000/api/download/分享码
# 下载文件(保持原文件名) curl -O -J http://localhost:8000/api/download/AB12CD34 # 下载并指定文件名 curl -o myfile.pdf http://localhost:8000/api/download/AB12CD34 # 下载到指定目录 curl -o ~/Downloads/file.txt http://localhost:8000/api/download/AB12CD34
-O:保存文件,使用服务器端文件名-J:使用服务器提供的文件名-o filename:指定保存的文件名在下载前查看文件的详细信息:
curl http://localhost:8000/api/info/分享码
curl http://localhost:8000/api/info/AB12CD34
{
"code": "AB12CD34",
"filename": "document.pdf",
"file_type": "application/pdf",
"size": 1048576,
"created_at": "2024-01-01T12:00:00",
"expires_at": "2024-01-01T12:15:00",
"is_expired": false
}
查看当前所有有效的分享:
curl http://localhost:8000/api/shares
# Linux/macOS 一行命令 curl -X POST -F "file=@myfile.txt" http://localhost:8000/api/upload | grep -o '"code":"[^"]*"' | cut -d'"' -f4 # 或使用jq解析JSON(需要安装jq) curl -X POST -F "file=@myfile.txt" http://localhost:8000/api/upload | jq -r '.code'
# 批量上传当前目录所有txt文件
for file in *.txt; do
echo "上传: $file"
curl -X POST -F "file=@$file" http://localhost:8000/api/upload
echo -e "\n---"
done
# 网络不稳定时使用重试 curl --retry 3 --retry-delay 1 -O -J http://localhost:8000/api/download/AB12CD34
# 上传时显示进度 curl --progress-bar -X POST -F "file=@largefile.zip" http://localhost:8000/api/upload # 下载时显示进度 curl --progress-bar -O -J http://localhost:8000/api/download/AB12CD34
# 404 - 分享码不存在
{
"detail": "分享码不存在"
}
# 410 - 分享已过期
{
"detail": "分享已过期"
}
# 413 - 文件太大
{
"detail": "文件太大,最大支持100MB"
}
-v 参数查看详细信息-w "%{http_code}" 查看HTTP状态码-s 参数静默模式(仅显示结果)如果服务部署在远程服务器,只需替换URL中的地址:
# 替换为您的服务器地址 curl -X POST -F "file=@myfile.txt" https://your-domain.com/api/upload # 或使用IP地址 curl -X POST -F "file=@myfile.txt" http://192.168.1.100:8000/api/upload