Go-POST提交图片文件&参数到远程服务器

// PostFormDataAndFile 提交头像到远程服务器
func PostFormDataAndFile(projectID, uuid string) (string, error) {
	client := http.Client{}
	bodyBuf := &bytes.Buffer{}
	defineConfig := global.GetConfig("define")
	postUrl := defineConfig.GetString("IMG_AVATAR_URL_" + projectID)
	imgPath := defineConfig.GetString("AVATAR_PATH")
	imageFilePath := imgPath + projectID + "-" + uuid + ".jpg"
	bodyWrite := multipart.NewWriter(bodyBuf)
	file, openErr := os.Open(imageFilePath)
	if openErr != nil {
		return "Open Error", openErr
	}

	defer file.Close()
	bodyWrite.WriteField("authKey", "*******")
	bodyWrite.WriteField("uuid", uuid)
	fileWrite, _ := bodyWrite.CreateFormFile("imgFile", uuid+".jpg")
	_, copyErr := io.Copy(fileWrite, file)
	if copyErr != nil {
		return "Copy Error", copyErr
	}

	bodyWrite.Close()
	contentType := bodyWrite.FormDataContentType()
	req, newRequestErr := http.NewRequest(http.MethodPost, postUrl+"appimg/setAvatar.php", bodyBuf)
	if newRequestErr != nil {
		return "NewRequest Error", newRequestErr
	}

	req.Header.Set("Content-Type", contentType) // 设置头
	resp, doErr := client.Do(req)
	if doErr != nil {
		return "Do Error", doErr
	}

	defer resp.Body.Close()
	result, readAllErr := ioutil.ReadAll(resp.Body)
	if readAllErr != nil {
		return "ReadAll Error", readAllErr
	}

	if removeErr := os.Remove(imageFilePath); removeErr != nil {
		return "Remove Error", removeErr
	}

	return string(result), nil
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java后端可以使用HttpClient库来实现Post提交文件流及参数的功能。示例代码如下: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // 添加文件流 builder.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, fileName); // 添加参数 builder.addTextBody("param1", "value1", ContentType.TEXT_PLAIN); builder.addTextBody("param2", "value2", ContentType.TEXT_PLAIN); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); CloseableHttpResponse response = httpClient.execute(httpPost); ``` 其中,`url`是要提交到的服务端地址,`file`是要提交文件流,`fileName`是文件名。`param1`和`param2`是要提交参数及其对应的值。 在服务端接收文件流及参数时,可以使用Spring框架的`MultipartFile`来接收文件流,使用`@RequestParam`来接收参数。示例代码如下: ```java @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("param1") String param1, @RequestParam("param2") String param2) throws IOException { byte[] bytes = file.getBytes(); // 处理文件流和参数 return "success"; } ``` 其中,`/upload`是服务端接收请求的地址,`file`是要接收的文件流,`param1`和`param2`是要接收的参数。 注意,在服务端接收参数时,需要根据参数的类型来设置参数的`ContentType`。例如,如果参数是文本类型,则设置为`ContentType.TEXT_PLAIN`。如果参数是JSON类型,则设置为`ContentType.APPLICATION_JSON`。 以上就是Java后端HttpClient Post提交文件流及参数的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luyaran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值