以下是使用安卓和Lua语言编写的上传指定目录下所有以.jpg为后缀的文件到指定服务器的代码示例:

-- 导入所需的模块
local lfs = require "lfs"
local socket = require "socket"
local http = require "socket.http"
local ltn12 = require "ltn12"

-- 指定目录和服务器信息
local directoryPath = "/sdcard/Pictures" -- 要上传的目录路径
local serverUrl = "http://example.com/upload" -- 服务器上传接口

-- 遍历指定目录下的所有.jpg文件并逐个上传
function uploadJPGFiles(directoryPath)
    for file in lfs.dir(directoryPath) do
        if file ~= "." and file ~= ".." then
            local filePath = directoryPath .. "/" .. file
            local attr = lfs.attributes(filePath)
            
            if attr.mode == "file" and string.lower(string.sub(file, -4)) == ".jpg" then
                uploadFile(filePath)
            elseif attr.mode == "directory" then
                uploadJPGFiles(filePath)
            end
        end
    end
end

-- 上传文件到服务器
function uploadFile(filePath)
    local file = io.open(filePath, "rb")
    if not file then
        print("无法打开文件:" .. filePath)
        return
    end
    
    local fileSize = lfs.attributes(filePath).size
    local fileData = file:read("*all")
    file:close()
    
    local response = {}
    local result, statusCode, headers, status = http.request {
        url = serverUrl,
        method = "POST",
        headers = {
            ["Content-Type"] = "image/jpeg",
            ["Content-Length"] = fileSize
        },
        source = ltn12.source.string(fileData),
        sink = ltn12.sink.table(response)
    }
    
    if statusCode == 200 then
        print("成功上传文件:" .. filePath)
    else
        print("上传文件失败:" .. filePath)
    end
end

-- 执行上传操作
uploadJPGFiles(directoryPath)

请确保已经安装了andlua模块,你可以在安卓设备上运行这段代码,修改directoryPath为你想要上传的目录路径,serverUrl为你的服务器上传接口地址。代码会遍历指定目录下所有以.jpg为后缀的文件,并逐个上传到指定的服务器。成功上传文件后,会在控制台输出相应的信息

帮我用安卓andlua使用Lua语言编写。主要功能是上传指定目录下的所有以jpg为后缀的文件到指定的服务器。

原文地址: http://www.cveoy.top/t/topic/iX3u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录