Maison > Article > développement back-end > 上传图片了就会报org.apache.http.client.HttpResponseException: Not Foun
一上传图片就会报上面的错, 的错,但是图片有地址,URL的地址是对的但是有图片有地址的,就是上不去,断点返回的是404.
一上传图片就会报上面的错, 的错,但是图片有地址,URL的地址是对的但是有图片有地址的,就是上不去,断点返回的是404.
404说明是客户端的问题导致服务器无法处理,https://segmentfault.com/a/11...
建议弃用httpclient,使用HttpURLConnection:
<code>import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; public class HttpConnector { public static final int TIMEOUT_MS = 10 * 1000; public static String performRequest(String url, File file, Map<string string> additionalHeaders) { String response = null; try { HashMap<string string> map = new HashMap<string string>(); if (additionalHeaders != null) map.putAll(additionalHeaders); URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, file); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } String charset = "utf-8"; String contentEncoding = null; String contentType = null; for (Map.Entry<string list>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { List<string> headerValueList = header.getValue(); if (headerValueList != null && headerValueList.size() > 0) { StringBuffer headerValueSB = new StringBuffer(); for (int i = 0; i </string></string></string></string></string></code>
调用:String response = HttpConnector.performRequest(uploadUrl, file, null);