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;
}
}
How to use Markdown to insert code
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 由于项目CI方面需求,前段时间学习和使用了微软的代码覆盖率(Code Coverage)分析工具,当时着手做时,发...
- 标题 效果: 这是一级标题 这是二级标题 这是三级标题 这是四级标题 这是五级标题 这是六级标题 列表 无序列表 ...
- Markdown is supported on many websites including Jianshu ...