当前位置:Gxlcms > PHP教程 > 上传图片了就会报org.apache.http.client.HttpResponseException:NotFoun

上传图片了就会报org.apache.http.client.HttpResponseException:NotFoun

时间:2021-07-01 10:21:17 帮助过:4人阅读

一上传图片就会报上面的错, 的错,但是图片有地址,URL的地址是对的但是有图片有地址的,就是上不去,断点返回的是404.

回复内容:

一上传图片就会报上面的错, 的错,但是图片有地址,URL的地址是对的但是有图片有地址的,就是上不去,断点返回的是404.

404说明是客户端的问题导致服务器无法处理,https://segmentfault.com/a/11...

建议弃用httpclient,使用HttpURLConnection:

  1. <code>import java.io.ByteArrayOutputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. public class HttpConnector {
  13. public static final int TIMEOUT_MS = 10 * 1000;
  14. public static String performRequest(String url, File file, Map<string, string=""> additionalHeaders) {
  15. String response = null;
  16. try {
  17. HashMap<string, string=""> map = new HashMap<string, string="">();
  18. if (additionalHeaders != null)
  19. map.putAll(additionalHeaders);
  20. URL parsedUrl = new URL(url);
  21. HttpURLConnection connection = openConnection(parsedUrl, file);
  22. for (String headerName : map.keySet()) {
  23. connection.addRequestProperty(headerName, map.get(headerName));
  24. }
  25. int responseCode = connection.getResponseCode();
  26. if (responseCode == -1) {
  27. // -1 is returned by getResponseCode() if the response code could not be retrieved.
  28. // Signal to the caller that something was wrong with the connection.
  29. throw new IOException("Could not retrieve response code from HttpUrlConnection.");
  30. }
  31. String charset = "utf-8";
  32. String contentEncoding = null;
  33. String contentType = null;
  34. for (Map.Entry<string, list<string="">> header : connection.getHeaderFields().entrySet()) {
  35. if (header.getKey() != null) {
  36. List<string> headerValueList = header.getValue();
  37. if (headerValueList != null && headerValueList.size() > 0) {
  38. StringBuffer headerValueSB = new StringBuffer();
  39. for (int i = 0; i < headerValueList.size(); i++) {
  40. headerValueSB.append(headerValueList.get(i));
  41. if (i != headerValueList.size() - 1) {
  42. headerValueSB.append(";");
  43. }
  44. }
  45. if (header.getKey().equals("Content-Type")) {
  46. contentType = headerValueSB.toString();
  47. } else if (header.getKey().equals("Content-Encoding")) {
  48. contentEncoding = headerValueSB.toString();
  49. }
  50. }
  51. }
  52. }
  53. charset = parseCharset(contentType, contentEncoding, charset);
  54. if (responseCode == 200) {
  55. //成功
  56. response = entityFromConnection(connection, charset);
  57. }
  58. } catch (Exception e) {
  59. // e.printStackTrace();
  60. }
  61. return response;
  62. }
  63. /**
  64. * Create an {@link HttpURLConnection} for the specified {@code url}.
  65. */
  66. protected static HttpURLConnection createConnection(URL url) throws IOException {
  67. return (HttpURLConnection) url.openConnection();
  68. }
  69. /**
  70. * Opens an {@link HttpURLConnection} with parameters.
  71. *
  72. * @param url
  73. * @return an open connection
  74. * @throws IOException
  75. */
  76. private static HttpURLConnection openConnection(URL url, File file) throws IOException {
  77. HttpURLConnection connection = createConnection(url);
  78. connection.setConnectTimeout(TIMEOUT_MS);
  79. connection.setReadTimeout(TIMEOUT_MS);
  80. connection.setUseCaches(false);
  81. connection.setDoInput(true);
  82. connection.setRequestMethod("POST");
  83. addBodyIfExists(connection, file);
  84. return connection;
  85. }
  86. private static String parseCharset(String contentType, String contentEncoding,
  87. String defaultCharset) {
  88. if (contentType != null) {
  89. String[] params = contentType.split(";");
  90. for (int i = 1; i < params.length; i++) {
  91. String[] pair = params[i].trim().split("=");
  92. if (pair.length == 2) {
  93. if (pair[0].equals("charset")) {
  94. return pair[1];
  95. }
  96. }
  97. }
  98. }
  99. if (contentEncoding != null && !contentEncoding.equals("")) {
  100. return contentEncoding;
  101. }
  102. return defaultCharset;
  103. }
  104. private static String entityFromConnection(HttpURLConnection connection, String charset) throws IOException {
  105. String data = null;
  106. InputStream inputStream;
  107. try {
  108. inputStream = connection.getInputStream();
  109. } catch (IOException ioe) {
  110. inputStream = null;
  111. }
  112. if (inputStream != null) {
  113. data = readData(inputStream, charset);
  114. }
  115. return data;
  116. }
  117. private static String readData(InputStream inSream, String charsetName) throws IOException {
  118. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  119. byte[] buffer = new byte[1024];
  120. int len = -1;
  121. while ((len = inSream.read(buffer)) != -1) {
  122. outStream.write(buffer, 0, len);
  123. }
  124. byte[] data = outStream.toByteArray();
  125. outStream.close();
  126. inSream.close();
  127. return new String(data, charsetName);
  128. }
  129. private static void addBodyIfExists(HttpURLConnection connection, File file) {
  130. try {
  131. if (file != null) {
  132. connection.setDoOutput(true);
  133. connection.addRequestProperty("Content-Type", "application/octet-stream; charset=utf-8");
  134. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
  135. FileInputStream fStream = new FileInputStream(file);
  136. /* 设定每次写入1024bytes */
  137. try {
  138. int bufferSize = 1024;
  139. byte[] buffer = new byte[bufferSize];
  140. int length = -1;
  141. /* 从文件读取数据到缓冲区 */
  142. while ((length = fStream.read(buffer)) != -1) {
  143. /* 将数据写入DataOutputStream中 */
  144. out.write(buffer, 0, length);
  145. }
  146. } finally {
  147. fStream.close();
  148. out.flush();
  149. out.close();
  150. }
  151. }
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. }</string></string,></string,></string,></string,></code>

调用:String response = HttpConnector.performRequest(uploadUrl, file, null);

人气教程排行