调用工具:资源下载

This commit is contained in:
huangge1199 2025-05-28 16:21:55 +08:00
parent 4d4b1fcf86
commit 74a610733f
4 changed files with 50 additions and 0 deletions

View File

@ -22,4 +22,6 @@ public interface ToolsService {
List<String> webScrap(String question); List<String> webScrap(String question);
String terminalTool(String command); String terminalTool(String command);
void downloadTool(String url, String name);
} }

View File

@ -74,4 +74,10 @@ public class ToolsServiceImpl implements ToolsService {
TerminalTool terminalTool = new TerminalTool(); TerminalTool terminalTool = new TerminalTool();
return terminalTool.executeTerminalCommand(command); return terminalTool.executeTerminalCommand(command);
} }
@Override
public void downloadTool(String url, String name) {
DownloadTool downloadTool = new DownloadTool();
downloadTool.downloadResource(url, name);
}
} }

View File

@ -86,4 +86,15 @@ public class ToolController {
String result = toolsService.terminalTool(command); String result = toolsService.terminalTool(command);
return R.ok(result); return R.ok(result);
} }
@PostMapping("/downloadTool")
@Operation(summary = "资源下载")
public R<?> downloadTool(@RequestBody JSONObject params) {
String url = params.getStr("url");
String name = params.getStr("name");
CheckUtils.checkEmpty(url, "url地址");
CheckUtils.checkEmpty(name, "文件名");
toolsService.downloadTool(url, name);
return R.ok();
}
} }

View File

@ -0,0 +1,31 @@
package com.huangge1199.aiagent.tools;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.io.File;
/**
* DownloadTool
*
* @author huangge1199
* @since 2025/5/28 16:11:48
*/
public class DownloadTool {
@Tool(description = "Download a resource from a given URL")
public String downloadResource(@ToolParam(description = "URL of the resource to download") String url, @ToolParam(description = "Name of the file to save the downloaded resource") String fileName) {
String fileDir = FileConstant.FILE_SAVE_DIR + "/download";
String filePath = fileDir + "/" + fileName;
try {
// 创建目录
FileUtil.mkdir(fileDir);
// 使用 Hutool downloadFile 方法下载资源
HttpUtil.downloadFile(url, new File(filePath));
return "Resource downloaded successfully to: " + filePath;
} catch (Exception e) {
return "Error downloading resource: " + e.getMessage();
}
}
}