Golang web 隐藏真实文件地址?

andyxiaqi · 2017-07-13 01:34:48 · 1162 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2017-07-13 01:34:48 的主题,其中的信息可能已经有所发展或是发生改变。

我想访问GO web 某个地址然后是下载文件。

例如我访问: http://127.0.0.1/public/image/img1 这个地址,然后下载文件,跟访问这个一样: http://127.0.0.1/public/image.jpg


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1162 次点击  
加入收藏 微博
5 回复  |  直到 2017-07-13 03:06:18
channel
channel · #1 · 8年之前

这不就是路由吗

andyxiaqi
andyxiaqi · #2 · 8年之前
channelchannel #1 回复

这不就是路由吗

是返回文件呀,主要是下载

shingle
shingle · #3 · 8年之前
package main

import (
    "io/ioutil"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/public/image/img1", a)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
func a(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "image/jpeg")
    b, _ := ioutil.ReadFile("a.png")
    w.Write(b)
}
andyxiaqi
andyxiaqi · #4 · 8年之前
shingleshingle #3 回复

``` package main import ( "io/ioutil" "log" "net/http" ) func main() { http.HandleFunc("/public/image/img1", a) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } func a(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/jpeg") b, _ := ioutil.ReadFile("a.png") w.Write(b) } ```

多谢多谢,我看看,那我不值图片,那我image/jpeg 改成什么

shingle
shingle · #5 · 8年之前
andyxiaqiandyxiaqi #4 回复

#3楼 @shingle 多谢多谢,我看看,那我不值图片,那我image/jpeg 改成什么

大哥,你还是学一下网络编程基础吧,关键词http header

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传