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

WBOY
发布: 2016-09-12 17:44:41
原创
1164 人浏览过

一上传图片就会报上面的错, 的错,但是图片有地址,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);

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!