All new technologies have experienced some growing pains, and blockchain technology is the same. However, although it is still at an early stage of development, it has great potential to improve many industries, many applications and systems.
The go language has its key features. The go language is so powerful because it always captures the programmer's itching needs in the development of the server, and solves and implements the problem in the most direct, simple, efficient and stable way.
Package Main
Import (
"Net/http"
"OS"
"IO"
"StrConv"
)
Func Main () {
F, err: = OS. OpenFile ("K:/file.mp3", OS. O_rdwr|os. O_create|os. O_append, 0666)
Stat, err: = F.stat ()//Get file status
If err! = Nil {panic (ERR)}//Put the file pointer to the end of the file, of course you say why not directly in the O_append mode open, yes yes. I'm just experimenting here.
URL: =
Req,_: = http. Newrequest ("GET", Url,nil);
Req. Header.set ("Range", "bytes=" + StrConv. Formatint (Stat. Size (), 10) + "-")
RESP, err: = http. Defaultclient.do (req)
If err! = Nil {panic (err)}
Written, err: = Io. Copy (f, resp. Body)
If err! = Nil {panic (err)}
println ("Written:", written)
}
Server, it is more simple, this is to ignore the URL of the/assets/, directly find the corresponding Raido directory
var Statichandler http. Handler
Static file Processing
Func staticserver (w http. Responsewriter, req *http. Request) {
Fmt. Println ("Path:" + req. Url. Path)
Statichandler.servehttp (W, req)
}
Func init () {
Statichandler = http. Stripprefix ("/assets/", http. Fileserver (http. Dir ("Radio")))
}
Func Main () {//already has a static file
http. Handlefunc ("/assets/", Staticserver)
ERR: = http. Listenandserve (":", nil)
If err! = Nil {
Log. Fatal ("Listenandserve:", err)
}
}
Handle multi-file uploads, if the post is in the form of an array, the back one [] do not forget, because Golang retains the bracket.
If the variables in the form are unique, use Parseformvalue directly, and Parsefile, because the return is a single variable instead of an array, save an array of other operations.
if R.method = = "POST" {
R.parsemultipartform (8 << 20)
Title: = r.parseformvalue["title"]
FHS: = r.multipartform.file["radio[]"
Options: = r.multipartform.value["options[]"
Answers: = r.multipartform.value["answers[]"
L: = len (options)
Optiondirs: = Make ([]string, L)
T: = time. Now ()
For I: = 0; I < L; i++ {
File, err: = Fhs[i]. Open ()
If err! = Nil {
Panic (ERR)
}
FileName: = Fhs[i]. Filename
F, err: = OS. OpenFile ("statics/" +filename, OS. O_wronly|os. O_create, 0666)
If err! = Nil {
Panic (ERR)
}
Defer F.close ()
Io. Copy (f, file)
Optiondirs = append (optiondirs, filename)
}
Db. Inserthomework (&db. homework{
Title:title,
Options:options,
Optiondirs:optiondirs,
Answers:answers,
Time:t,
})
Sess: = Session. Globalsessionmanager.sessionstart (W, R)
If sess! = Nil {
Sess. Set ("Flash", true)
}
Defer Sess. Sessionrelease ()
http. Redirect (W, R, "/homeworks", http. Statusfound)
}
high-energy early warning, brother even education blockchain live course August continues to hit the hot!
The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!
Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!!
Limited time limit!! First come first served!!
Http://www.ydma.cn/open/course/24
Follow brother Lian Blockchain technology public number get more technical dry Goods Oh!!!