📡 curl命令使用教程

无需安装任何工具,使用系统自带的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"
}
💡 提示:
  • 文件路径支持绝对路径和相对路径
  • 最大支持100MB文件
  • 分享码为8位大写字母+数字组合

📝 分享文本

直接分享文本内容,无需创建文件。提供多种简化方式:

✨ 方式1: 超级简单(推荐)

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

🔧 方式2: 表单方式(可指定文件名)

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

📋 方式3: JSON方式(兼容性)

curl -X POST -H "Content-Type: application/json" \
  -d '{"content":"文本内容","filename":"文件名.txt"}' \
  http://localhost:8000/api/share-text
💡 推荐使用顺序:
  • ✨ 超级简单方式 - 最短命令,适合快速分享
  • 🔧 表单方式 - 需要自定义文件名时使用
  • 📋 JSON方式 - 需要更复杂控制时使用

⬇️ 下载文件

使用分享码下载文件到本地:

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
← 返回Web界面