go文件服务器mimetype,解决golang post文件时Content-Type出现的问题

本文讲述了在Golang中使用http.Post上传文件时遇到Content-Type问题,通过分析源码发现默认设置为application/octet-stream,而实际需要text/plain。提出了两种解决方案,一种是修改源码,另一种是自定义创建Part并设置Content-Type。还补充介绍了Go搭建简单Web服务器时,服务器如何根据响应内容自动设置Content-Type,并强调了让程序员明确指定Content-Type的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

同事用php写了一个接口,要上传文件,让我做下测试,直接用curl命令调用成功,然后想用golang写个示例,

源码如下:

package main

import (

"bytes"

"fmt"

"io/ioutil"

"mime/multipart"

"net/http"

)

func main() {

uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商务提供

name := "xxxxxxxxxxxx" //用户名

pass := "xxxxxxxxxxxx" //密码

fn := "xxxxxxxxxxxx.txt" //文件路径

//读出文本文件数据

file_data, _ := ioutil.ReadFile(fn)

body := new(bytes.Buffer)

w := multipart.NewWriter(body)

//取出内容类型

content_type := w.FormDataContentType()

//将文件数据写入

pa, _ := w.CreateFormFile("file", fn)

pa.Write(file_data)

//设置用户名密码

w.WriteField("name", name)

w.WriteField("pass", pass)

w.Close()

//开始提交

req, _ := http.NewRequest("POST", uri, body)

req.Header.Set("Content-Type", content_type)

resp, _ := http.DefaultClient.Do(req)

data, _ := ioutil.ReadAll(resp.Body)

resp.Body.Close()

fmt.Println(resp.StatusCode)

fmt.Printf("%s", data)

}

发现总是调用失败,返回文件类型不对,询问后得知,同事做了判断,文件只能为text/plain类型,抓包发现,我提交时的文件类型为:application/octet-stream,仔细查看golang源码:mime/multipart/write.go,CreateFormFile的源码是这样的:

func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error) {

h := make(textproto.MIMEHeader)

h.Set("Content-Disposition",

fmt.Sprintf(`form-data; name="%s"; filename="%s"`,

escapeQuotes(fieldname), escapeQuotes(filename)))

h.Set("Content-Type", "application/octet-stream")

return w.CreatePart(h)

}

可以得知Content-Type被固定为了application/octet-stream,知道原因了,问题就好解决了。

第一种方法

就是直接修改CreateFormFile,或者加个CreateFormFile2命令,这种方法将来golang升级后可能会出问题。

第二种方法

可以自己来CreatePart:

h := make(textproto.MIMEHeader)

h.Set("Content-Disposition",

fmt.Sprintf(`form-data; name="%s"; filename="%s"`,

escapeQuotes(fieldname), escapeQuotes(filename)))

h.Set("Content-Type", "text/plain")

再用 w.CreatePart(h)得到io.Writer,问题解决!这种方法不侵入golang源代码,最终代码如下:

package main

import (

"bytes"

"fmt"

"io/ioutil"

"mime/multipart"

"net/http"

"net/textproto"

)

func main() {

uri := "http://xxxxxxxxxxxx/api/fileattr" //URL地址 xxxxxxxxxxxx由商务提供

name := "xxxxxxxxxx" //用户名

pass := "xxxxxxx" //密码

fn := "x:/xxx/xxx.txt" //文件路径

//读出文本文件数据

file_data, _ := ioutil.ReadFile(fn)

body := new(bytes.Buffer)

w := multipart.NewWriter(body)

//取出内容类型

content_type := w.FormDataContentType()

//将文件数据写入

h := make(textproto.MIMEHeader)

h.Set("Content-Disposition",

fmt.Sprintf(`form-data; name="%s"; filename="%s"`,

"file", //参数名为file

fn))

h.Set("Content-Type", "text/plain") //设置文件格式

pa, _ := w.CreatePart(h)

pa.Write(file_data)

//设置用户名密码

w.WriteField("name", name)

w.WriteField("pass", pass)

w.Close()

//开始提交

req, _ := http.NewRequest("POST", uri, body)

req.Header.Set("Content-Type", content_type)

resp, _ := http.DefaultClient.Do(req)

data, _ := ioutil.ReadAll(resp.Body)

resp.Body.Close()

fmt.Println(resp.StatusCode)

fmt.Printf("%s", data)

}

补充:用go来玩最简单的web服务器------顺便说说Content-Type字段

web服务端代码s.go:

package main

import (

"io"

"log"

"net/http"

)

func handlerHello(w http.ResponseWriter, r *http.Request) {

io.WriteString(w, "hello girls")

}

func main() {

http.HandleFunc("/hello", handlerHello) // 注册

err := http.ListenAndServe("localhost:8080", nil)

if err != nil {

log.Println(err)

}

}

go run s.go一下,跑起来, 然后在浏览器执行http://127.0.0.1:8080/hello (或者在命令行用curl发http请求也可以), 浏览器上的结果为:

hello girls

好简单。可以在客户端或者服务端抓包看下, 很典型的http req和rsp.

我们再来看一个有趣的问题, 修改s.go为:

package main

import (

"io"

"log"

"net/http"

)

func handlerHello(w http.ResponseWriter, r *http.Request) {

str := `

table border="1">

row 1, cell 1row 1, cell 2row 2, cell 1row 2, cell 2

`

io.WriteString(w, str)

}

func main() {

http.HandleFunc("/hello", handlerHello) // 注册

err := http.ListenAndServe("localhost:8080", nil)

if err != nil {

log.Println(err)

}

}

再次重启服务并发请求, 浏览器上显示的内容是:

table border="1">

row 1, cell 1row 1, cell 2row 2, cell 1row 2, cell 2

抓包看一下, 发现有:Content-Type: text/plain; charset=utf-8

因此, 浏览器需要根据纯文本显示。 注意到, 上述的table左边少了一个"

s.go的代码如下:

package main

import (

"io"

"log"

"net/http"

)

func handlerHello(w http.ResponseWriter, r *http.Request) {

str := `

row 1, cell 1row 1, cell 2
row 2, cell 1row 2, cell 2

`

io.WriteString(w, str)

}

func main() {

http.HandleFunc("/hello", handlerHello) // 注册

err := http.ListenAndServe("localhost:8080", nil)

if err != nil {

log.Println(err)

}

}

再次重启服务,发请求,浏览器端的显示是:

row 1, cell 1

row 1, cell 2

row 2, cell 1

row 2, cell 2

抓包看, 有Content-Type: text/html; charset=utf-8

可见, 服务端会判断str的格式,来确定Content-Type的类型, 从而决定了浏览器端的展示方式。服务端的自动判断行为, 有点意思。 在我看来, 这样不太好,应该让程序员来指定Content-Type.

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:golang post请求常用的几种方式小结

在golang xorm中使用postgresql的json,array类型的操作

golang使用http client发起get和post请求示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值