Go图片列表

需求

在一个页面浏览目录下所有图片

代码

package main

import (
	"net/http"
	"fmt"
	"io/ioutil"
	"sort"
	"strings"
	"strconv"
)

func handleRequest(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.Proto + " " + r.Host + " " +r.RequestURI)
	dir, err := ioutil.ReadDir("." + r.RequestURI)
	
	//排序
	sort.Slice(dir, func(i, j int) bool {
		di := dir[i]
        dj := dir[j]
        if di.IsDir() && !dj.IsDir() { // 目录在前
            return true
        } else if !di.IsDir() && dj.IsDir() { // 目录在后
            return false
        }
        //return dir[i].Name() < dir[j].Name() //和WIN7不一样
		return dir[i].ModTime().Before(dir[j].ModTime())
    })
	
	if err != nil {
		http.ServeFile(w, r, "." + r.RequestURI);		
	} else {
		path := "/"
		if (r.RequestURI != "/") {
			n := strings.LastIndex(r.RequestURI, "/");
			if (n != 0) {
				path = string(r.RequestURI[:n])
			}
		}
		ddir := ""
		if (r.RequestURI != "/") {
			ddir = " <a href=''>删除此目录</a>"
		}
		str := "<html>\n<head>\n<title>文件列表</title>\n</head>\n<style>\na, img { -webkit-user-drag:none; }\na { text-decoration:none; padding:10px; margin:10px; background:lightgray; display:inline-block; }\na:hover { background:gray; }\nimg { max-width: 100%; }\n</style>\n<body>\n<p><a href='" + path + "'>[" + path + "]</a>" + ddir + "</p>"
        path = "/"
		if (r.RequestURI != "/") {
			path = r.RequestURI + "/"
		}
		var count_dir, count_file int
		for _, file := range dir {
			if (file.IsDir()) {				
				str += "<a href='" + path + file.Name() + "'>" + file.Name() + "</a>\n"
				count_dir++
			} else {			
				str += "<img src='" + path + file.Name() +"'>\n"				
				count_file++
			}
		}	
		str += "<p>目录" + strconv.Itoa(count_dir) + "个,文件" + strconv.Itoa(count_file) + "个</p>\n</body>\n</html>"
		w.Write([]byte(str))
	}
}

func main() {	
	fmt.Println("http://localhost:8000");
	http.HandleFunc("/", handleRequest);
	http.ListenAndServe(":8000", nil);
}

问题

1.不支持非英文目录

2.删除目录没有实现

3.根据文件头判断文件类型进行不同处理没有实现

相关推荐

  1. Go图片列表

    2024-05-03 05:44:03       33 阅读
  2. Go 数据结构】列表与字典

    2024-05-03 05:44:03       31 阅读
  3. Qt 富文本 表格列表图片

    2024-05-03 05:44:03       24 阅读
  4. 虾皮Shopee API接口获取商品图片列表

    2024-05-03 05:44:03       65 阅读
  5. Go语言介绍及Go语言成功的项目列举

    2024-05-03 05:44:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-03 05:44:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 05:44:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 05:44:03       82 阅读
  4. Python语言-面向对象

    2024-05-03 05:44:03       91 阅读

热门阅读

  1. Spring 之 MatchingStrategy

    2024-05-03 05:44:03       37 阅读
  2. 应用zabbix的实时导出(real-time export)功能

    2024-05-03 05:44:03       32 阅读
  3. 定时器使用

    2024-05-03 05:44:03       24 阅读
  4. Web开发之上传图片

    2024-05-03 05:44:03       26 阅读
  5. CANopen学习笔记

    2024-05-03 05:44:03       27 阅读
  6. 揭秘靠信息差搞钱的三个步骤!

    2024-05-03 05:44:03       29 阅读
  7. 快速了解Linux IPC

    2024-05-03 05:44:03       30 阅读
  8. 嵌入式-进程、线程

    2024-05-03 05:44:03       30 阅读
  9. PostgresQL-丢失各种数据文件如何恢复

    2024-05-03 05:44:03       29 阅读
  10. AtCoder ABC351 A-D题解

    2024-05-03 05:44:03       32 阅读
  11. 【无标题】

    2024-05-03 05:44:03       32 阅读