欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-18 22:38:23
...
一上传图片就会报上面的错, 的错,但是图片有地址,URL的地址是对的但是有图片有地址的,就是上不去,断点返回的是404.

回复内容:

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

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

建议弃用httpclient,使用HttpURLConnection:

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 additionalHeaders) {
        String response = null;
        try {
            HashMap map = new HashMap();
            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> header : connection.getHeaderFields().entrySet()) {
                if (header.getKey() != null) {
                    List headerValueList = header.getValue();
                    if (headerValueList != null && headerValueList.size() > 0) {
                        StringBuffer headerValueSB = new StringBuffer();
                        for (int i = 0; i 

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

相关标签: android php