| | |
| | | return "",err |
| | | } |
| | | return "http://"+m["url"].(string)+"/"+m["fid"].(string),err |
| | | } |
| | | |
| | | func UpDateFile(uri string, fileName string, fileData []byte) (m map[string]interface{}, err error) { |
| | | body := &bytes.Buffer{} |
| | | writer := multipart.NewWriter(body) |
| | | _, err = writer.CreateFormFile("file", fileName) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | boundary := writer.Boundary() |
| | | //close_string := fmt.Sprintf("\r\n--%s--\r\n", boundary) |
| | | close_buf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary)) |
| | | file := bytes.NewBuffer(fileData) |
| | | request_reader := io.MultiReader(body, file, close_buf) |
| | | //_, err = io.Copy(part, file) |
| | | //writer.WriteField(key, val) |
| | | request, err := http.NewRequest("POST", uri, request_reader) |
| | | request.Header.Add("Content-Type", writer.FormDataContentType()) |
| | | timeout := time.Duration(5 * time.Second) //超时时间50ms |
| | | client := &http.Client{Timeout: timeout} |
| | | resp, err := client.Do(request) |
| | | if err != nil { |
| | | fmt.Println("UploadFile client.Do(request) err:", err) |
| | | return nil, err |
| | | } |
| | | defer func() { |
| | | if r := recover(); r != nil { |
| | | fmt.Printf("panic的内容%v\n", r) |
| | | msg := "上传图片服务器异常" |
| | | if _, ok := r.(error); ok { |
| | | msg = r.(error).Error() |
| | | fmt.Println("panic--recover()得到的是error类型") |
| | | } |
| | | if _, ok := r.(string); ok { |
| | | msg = r.(string) |
| | | fmt.Println("panic--recover()得到的是string类型") |
| | | } |
| | | err = errors.New(msg) |
| | | } |
| | | }() |
| | | defer resp.Body.Close() |
| | | { |
| | | body := &bytes.Buffer{} |
| | | _, err := body.ReadFrom(resp.Body) |
| | | if err != nil { |
| | | fmt.Println("UploadFile body.ReadForm(resp.Body) err:", err) |
| | | } |
| | | |
| | | m := make(map[string]interface{}) |
| | | if err := json.Unmarshal([]byte(body.String()), &m); err != nil { |
| | | return nil, err |
| | | } |
| | | return m,nil |
| | | } |
| | | } |