• golang 文件流转发


    ....占位

    copy from  

    https://www.jianshu.com/p/10c857270947

    https://www.jianshu.com/p/55b0b8126e40?utm_campaign=studygolang.com&utm_medium=studygolang.com&utm_source=studygolang.com

    https://www.codercto.com/a/48060.html

     1 // dataServer.go
     2 package main
     3  
     4 import (
     5     "io"
     6     "log"
     7     "net/http"
     8     "os"
     9     "strings"
    10 )
    11  
    12 const objectDir = "./objects/"
    13  
    14 var (
    15     url = "http://1.1.13.10:9800/huzh1310.txt"
    16 )
    17  
    18 func Handler(w http.ResponseWriter, r *http.Request) {
    19     m := r.Method
    20     if m == http.MethodGet {
    21         get(w, r)
    22         return
    23     }
    24     w.WriteHeader(http.StatusMethodNotAllowed)
    25     w.Header().Set("content-disposition", "attachment;filename=aa.txt")
    26 }
    27  
    28 func get(w http.ResponseWriter, r *http.Request) {
    29     // 收到 接口服务器的 请求
    30     // 提取 要获取的文件名
    31     name := strings.Split(r.URL.EscapedPath(), "/")[2]
    32     f, e := os.Open(objectDir + name)
    33     if e != nil {
    34         log.Println(e)
    35         w.WriteHeader(http.StatusNotFound)
    36         return
    37     }
    38     defer f.Close()
    39     res, err := http.Get(url)
    40     if err != nil {
    41         panic(err)
    42     }
    43     // 真正读取,并发送
    44  
    45     //文件流写入
    46     //io.Copy(w, f)
    47  
    48     //http 流写入
    49     io.Copy(w, res.Body)
    50 }
    51  
    52 func getURL() {
    53     res, err := http.Get(url)
    54     if err != nil {
    55         panic(err)
    56     }
    57     f, err := os.Create("huzh1310_local.txt")
    58     if err != nil {
    59         panic(err)
    60     }
    61     io.Copy(f, res.Body)
    62 }
    63  
    64 func main() {
    65     http.HandleFunc("/objects/", Handler)
    66     http.ListenAndServe(":8889", nil)
    67     getURL()
    68  
    69 }
    // dataServer.go
    package main
    
    import (
    	"io"
    	"log"
    	"net/http"
    	"os"
    	"strings"
    )
    
    const objectDir = "./objects/"
    
    var (
    	url = "http://1.1.13.10:9800/huzh1310.txt"
    )
    
    func Handler(w http.ResponseWriter, r *http.Request) {
    	m := r.Method
    	if m == http.MethodGet {
    		get(w, r)
    		return
    	}
    	w.WriteHeader(http.StatusMethodNotAllowed)
    	w.Header().Set("content-disposition", "attachment;filename=aa.txt")
    }
    
    func get(w http.ResponseWriter, r *http.Request) {
    	// 收到 接口服务器的 请求
    	// 提取 要获取的文件名
    	name := strings.Split(r.URL.EscapedPath(), "/")[2]
    	f, e := os.Open(objectDir + name)
    	if e != nil {
    		log.Println(e)
    		w.WriteHeader(http.StatusNotFound)
    		return
    	}
    	defer f.Close()
    	res, err := http.Get(url)
    	if err != nil {
    		panic(err)
    	}
    	// 真正读取,并发送
    
    	//文件流写入
    	//io.Copy(w, f)
    
    	//http 流写入
    	io.Copy(w, res.Body)
    }
    
    func getURL() {
    	res, err := http.Get(url)
    	if err != nil {
    		panic(err)
    	}
    	f, err := os.Create("huzh1310_local.txt")
    	if err != nil {
    		panic(err)
    	}
    	io.Copy(f, res.Body)
    }
    
    func main() {
    	http.HandleFunc("/objects/", Handler)
    	http.ListenAndServe(":8889", nil)
    	getURL()
    
    }
    

      

     待补充, golang http response header

  • 相关阅读:
    设计模式之观察者模式
    设计模式之建造者模式
    设计模式之外观模式
    设计模式之模板方法模式
    设计模式之原型模式
    自己动手写计算器v1.1
    自己动手写计算器v1.0
    Guid的使用
    设计模式学习---代理类
    StringBuilder的使用
  • 原文地址:https://www.cnblogs.com/eiguleo/p/13876228.html
Copyright © 2020-2023  润新知