How to use Markdown to insert code

4249 단어
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;

}

}

좋은 웹페이지 즐겨찾기