当前位置:   article > 正文

Gradle http Post 文件上传 、Get_gradio 上传文件

gradio 上传文件

 一、使用 HttpURLConnection

  1. // post文字
  2. static def httpPostText(String url, String text) {
  3. return doRequest(url, { HttpURLConnection post ->
  4. post.setRequestProperty("content-length", text.length().toString())
  5. post.setRequestProperty("content-type", "application/x-www-form-urlencoded")
  6. }) { DataOutputStream dos ->
  7. dos.writeBytes(text)
  8. }
  9. }
  10. // 上传文件
  11. static def httpUploadFile(String url, File fileUpload) {
  12. String boundary = UUID.randomUUID().toString()
  13. return doRequest(url, { HttpURLConnection post ->
  14. post.setRequestProperty("content-length", fileUpload.size().toString())
  15. post.setRequestProperty("content-type", "multipart/form-data;boundary=$boundary")
  16. }) { DataOutputStream dos ->
  17. dos.writeBytes("--$boundary\r\n")
  18. dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"${fileUpload.name}\"\r\n\r\n")
  19. fileUpload.eachByte(1024) { byte[] bytes, Integer len -> dos.write(bytes, 0, len) }
  20. dos.writeBytes("\r\n--$boundary--\r\n")
  21. }
  22. }
  23. /**
  24. * cloOnRequest: 填充请求参数
  25. * cloOnPost: 具体的post 内容
  26. **/
  27. static def doRequest(String url, Closure cloOnRequest, Closure cloOnPost) {
  28. HttpURLConnection post = null
  29. int respCode = 0
  30. String respText = ""
  31. try {
  32. post = new URL(url).openConnection()
  33. post.requestMethod = cloOnPost != null? "POST": "GET"
  34. post.useCaches = false
  35. post.doOutput = true
  36. post.readTimeout = 10000
  37. post.connectTimeout = 10000
  38. post.setRequestProperty("cache-control", "no-cache")
  39. // 添加其他的参数
  40. if (cloOnRequest != null) {
  41. cloOnRequest.call(post)
  42. }
  43. if (cloOnPost != null) {
  44. DataOutputStream dos = new DataOutputStream(post.outputStream)
  45. cloOnPost.call(dos)
  46. dos.flush()
  47. dos.close()
  48. }
  49. respCode = post.responseCode
  50. respText = post.getInputStream().text.trim()
  51. } finally {
  52. if (post != null) post.disconnect()
  53. }
  54. return ["code": respCode, "text": respText]
  55. }

二、使用 apache httpclient

  1. buildscript {
  2. repositories {
  3. mavenCentral()
  4. }
  5. dependencies {
  6. classpath 'org.apache.httpcomponents:httpclient:4.5.9'
  7. classpath 'org.apache.httpcomponents:httpmime:4.5.9'
  8. }
  9. }
  10. import org.apache.http.HttpEntity
  11. import org.apache.http.client.methods.*
  12. import org.apache.http.impl.client.*
  13. import org.apache.http.util.EntityUtils
  14. import org.apache.http.entity.mime.MultipartEntityBuilder
  15. static def httpUploadFile(String url, File fileUpload) {
  16. return doRequest(url, MultipartEntityBuilder.create().addBinaryBody("file", fileUpload).build())
  17. }
  18. static def httpPostText(String url, String name, String text) {
  19. return doRequest(url, MultipartEntityBuilder.create().addTextBody(name, text).build())
  20. }
  21. static def doRequest(String url, HttpEntity requestEntity) {
  22. CloseableHttpResponse response = null
  23. CloseableHttpClient httpClient = null
  24. int respCode = 0
  25. String respText = ""
  26. try {
  27. httpClient = HttpClientBuilder.create().build()
  28. HttpRequestBase request
  29. if (requestEntity == null) {
  30. request = new HttpGet(url)
  31. } else {
  32. request = new HttpPost(url)
  33. request.setEntity(requestEntity)
  34. }
  35. // 添加header: request.setHeader()
  36. response = httpClient.execute(request)
  37. def entity = response.getEntity()
  38. respCode = response.statusLine.statusCode
  39. respText = EntityUtils.toString(response.getEntity(), "utf-8").trim()
  40. EntityUtils.consume(entity)
  41. } finally {
  42. if (httpClient != null) httpClient.close()
  43. if (response != null) response.close()
  44. }
  45. return ["code": respCode, "text": respText]
  46. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/132484
推荐阅读
相关标签
  

闽ICP备14008679号