How to use Markdown to insert code

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 
 *             請求網(wǎng)絡(luò)的URL
 * @param requestMethod
 *             請求方法 GET || POST
 * @param filePath
 *             需要上傳的圖片的所在位置
 * @param formatName
 *             上傳到服務(wù)器后的圖片格式 eg: .JPG .PNG ...
 * @return
 *          返回json格式的數(shù)據(jù)
 */
public static JSONObject httpRequest(String requestUrl, String requestMethod, String filePath, String formatName) {

    // 方法返回數(shù)據(jù),JSON格式
    JSONObject jsonObject = null;
    // 存放服務(wù)器返回的輸入流轉(zhuǎn)化成字符串
    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);
           //讀取圖片進(jìn)行解碼
            BufferedImage bm = ImageIO.read(imgFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //將內(nèi)存中的圖片按照某種格式進(jìn)行格式化
            ImageIO.write(bm, formatName, bos);
            bos.flush();
            byte[] data = bos.toByteArray();
            outputStream.write(data);
            outputStream.flush();
            outputStream.close();

        }

        // 獲取結(jié)果
        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);
        }

        // 結(jié)束操作,關(guān)閉
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        inputStream = null;
        httpUrlConn.disconnect();

        // 結(jié)果數(shù)據(jù)轉(zhuǎn)化成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;

}

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容