获取src下文件路径(本地+服务器)

        //获取src下文件所在本地路径

public static String getFilePath2(String _fileName) {
File directory = new File("");// 参数为空
String _filePath = null;
try {
_filePath = directory.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return _filePath + "\\src\\" + _fileName;

}


 //获取src下文下所在服务器路径
public static String getFilePath(String _fileName) {
// String

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 HTML 中无法直接获取本地文件夹下的文件列表,因为 HTML 运行在浏览器中,受到了安全限制,无法直接访问本地文件系统。但是,你可以使用 JavaScript 通过 AJAX 或 Fetch API 向服务器请求文件列表,然后在 HTML 中显示。 以下是一个使用 JavaScript 和 HTML 的示例,可以请求服务器上指定文件夹中的所有图片文件,并在网页上显示这些图片: ```html <!DOCTYPE html> <html> <head> <title>Show Images</title> </head> <body> <div id="image-container"></div> <script> // 指定要请求的文件路径 var folder_path = "/path/to/folder"; // 发送 AJAX 请求,获取文件列表 var xhr = new XMLHttpRequest(); xhr.open("GET", "/get-files.php?path=" + encodeURIComponent(folder_path)); xhr.onload = function() { if (xhr.status === 200) { var files = JSON.parse(xhr.responseText); displayImages(files); } else { console.log("Error retrieving image files: " + xhr.statusText); } }; xhr.send(); // 在网页上显示图片 function displayImages(files) { var container = document.getElementById("image-container"); for (var i=0; i<files.length; i++) { if (files[i].endsWith(".jpg") || files[i].endsWith(".jpeg") || files[i].endsWith(".png")) { var img = document.createElement("img"); img.src = files[i]; container.appendChild(img); } } } </script> </body> </html> ``` 这个 HTML 页面中,我们先使用 JavaScript 发送 AJAX 请求,请求服务器上指定文件夹下的所有文件列表。服务器端的代码可以使用 PHP、Python 等语言实现,这里使用 PHP 代码示例: ```php <?php $path = $_GET["path"]; $files = array(); if ($handle = opendir($path)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $files[] = $path . "/" . $entry; } } closedir($handle); } echo json_encode($files); ?> ``` 这个 PHP 脚本会读取指定文件夹下的所有文件,并将文件列表以 JSON 格式返回给 JavaScript。最后,JavaScript 解析返回的 JSON 数据,筛选出图片文件,并将它们显示在 HTML 页面中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q5515069

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值