已解决:微信小店图片上传失败 {"errcode":-1,"errmsg":"system error"}

已解决:微信小店图片上传失败 {
uploadpic-error.png

解决:
微信小店图片上传主要代码:

package fuck.shit.uploadpic;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinygroup.commons.tools.StringUtil;

public class UplaodPicUtil {

private static Logger log = LoggerFactory.getLogger(UplaodPicUtil.class);

/**
 * 
 * @param requestUrl 
 *          请求网络的URL
 * @param requestMethod
 *          请求方法 GET || POST
 * @param filePath
 *          需要上传的图片的所在位置
 * @param formatName
 *          上传到服务器后的图片格式 eg: .JPG .PNG ...
 * @return
 *          返回json格式的数据
 */
public static JSONObject httpRequest(String requestUrl, String requestMethod, String filePath, String formatName) {

    // 方法返回数据,JSON格式
    JSONObject jsonObject = null;
    // 存放服务器返回的输入流转化成字符串
    StringBuffer buffer = new StringBuffer();

    try {

        TrustManager[] tm = { new MyX509TrustManager() };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        SSLSocketFactory ssf = sslContext.getSocketFactory();

        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
        httpUrlConn.setSSLSocketFactory(ssf);
        httpUrlConn.setDoOutput(true);
        httpUrlConn.setDoInput(true);
        httpUrlConn.setUseCaches(false);
        httpUrlConn.setRequestMethod(requestMethod);
        httpUrlConn.setRequestProperty("Connection", "Keep-Alive");
        httpUrlConn.setRequestProperty("Charset", "UTF-8");
        httpUrlConn.setRequestProperty("Content-type", "application/x-java-serialized-object");

        if ("GET".equalsIgnoreCase(requestMethod))
            httpUrlConn.connect();

        // 图片路径不能为空,图片格式不能为空
        if (!StringUtil.isEmpty(filePath) && !StringUtil.isEmpty(formatName)) {

            OutputStream outputStream = httpUrlConn.getOutputStream();
            File imgFile = new File(filePath);
           //读取图片进行解码
            BufferedImage bm = ImageIO.read(imgFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //将内存中的图片按照某种格式进行格式化
            ImageIO.write(bm, formatName, bos);
            bos.flush();
            byte[] data = bos.toByteArray();
            outputStream.write(data);
            outputStream.flush();
            outputStream.close();

        }

        // 获取结果
        InputStream inputStream = httpUrlConn.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
        }

        // 结束操作,关闭
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();

        // 结果数据转化成JSON格式
        jsonObject = JSONObject.fromObject(buffer.toString());

    } catch (ConnectException ce) {
        log.error("Weixin server connection timed out.");
    } catch (Exception e) {
        log.error("https request error:{}", e);
    }
    return jsonObject;
}
}

class MyX509TrustManager implements X509TrustManager {

public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

public X509Certificate[] getAcceptedIssuers() {

    return null;

}

}

你可能感兴趣的:(已解决:微信小店图片上传失败 {"errcode":-1,"errmsg":"system error"})