Home
Categories
WIKI
Topic
User
LANGUAGE:
中文
English
【Go语言教学】相册文件服务器
社区开发
679
views ·
0
replies ·
To
floor
Go
海天鹰
deepin
2019-08-06 01:23
Author
功能:目录生成链接,其他文件生成图片。
已知问题:
1.http.HandleFunc 如何设置绝对路径?
2.outil.ReadDir 和图片无法打开含空格或者中文的路径。
3.速度慢。
package main
import (
"net/http"
"fmt"
"io/ioutil"
"strings"
)
func handleRequest(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Proto + " " + r.Host + r.RequestURI);
dir := "." + r.RequestURI;
strings.Replace(dir, "%20", " ", -1);
files, err := ioutil.ReadDir(dir);
fmt.Println("genList(" + dir + ")");
if err != nil {
fmt.Println("Line 17: " + err.Error());
http.ServeFile(w, r, "." + r.RequestURI);
} else {
str := "\n\n文件列表\n\n\n\n";
for _, file := range files {
rp := "";
if (r.RequestURI != "/") {
rp = r.RequestURI;
}
if (file.IsDir()) {
str += "
" + file.Name() + "
\n";
} else {
str += "
\n";
}
}
str += "\n";
w.Write([]byte(str));
}
}
func main() {
fmt.Println("http://localhost:8000");
http.HandleFunc("/", handleRequest);
http.ListenAndServe(":8000", nil);
}
Reply
Like 0
Favorite
View the author
All Replies
No replies yet
Please
sign
in first
Featured Collection
Change
[Tutorial] deepin25 WSL Offline Installation Guide
UOS AI 2.8 Released! Three New Intelligent Agents & Major Evolution
Solid Q&A | deepin 25 Common Questions – The Immutable System Edition
New Thread
Popular Ranking
Change
What is the purpose of UOS AI?
Cannot upgrade "Deepin 25.0.10"
Experience
Feature Request: Adding an option for a "Floating Dock" mode in DDE
[Feature Request] Drop-down grid panel for window snap layouts
Popular Events
More
已知问题:
1.http.HandleFunc 如何设置绝对路径?
2.outil.ReadDir 和图片无法打开含空格或者中文的路径。
3.速度慢。
package main
import (
"net/http"
"fmt"
"io/ioutil"
"strings"
)
func handleRequest(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Proto + " " + r.Host + r.RequestURI);
dir := "." + r.RequestURI;
strings.Replace(dir, "%20", " ", -1);
files, err := ioutil.ReadDir(dir);
fmt.Println("genList(" + dir + ")");
if err != nil {
fmt.Println("Line 17: " + err.Error());
http.ServeFile(w, r, "." + r.RequestURI);
} else {
str := "\n\n文件列表\n\n\n\n";
for _, file := range files {
rp := "";
if (r.RequestURI != "/") {
rp = r.RequestURI;
}
if (file.IsDir()) {
str += "
" + file.Name() + "
\n";} else {
str += "
}
}
str += "\n";
w.Write([]byte(str));
}
}
func main() {
fmt.Println("http://localhost:8000");
http.HandleFunc("/", handleRequest);
http.ListenAndServe(":8000", nil);
}