HttpKit支持文件上传 , 标题党一下, 这里分享一下我包装的工具类,直接上石马~
- package com.momathink.update.kit;
- import java.io.BufferedReader;
- import java.io.Closeable;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.Charset;
- import java.security.SecureRandom;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.HashMap;
- import java.util.Map;
- import javax.net.ssl.HostnameVerifier;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import com.jfinal.kit.HttpKit;
- import com.jfinal.kit.JsonKit;
- public class UpdateHttp {
- private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
- /**
- * 上传代码来自 jfinal-weixin
- *
- * @param url 上传网址
- * @param file 上传的文件
- * @return ApiResult
- * @throws IOException
- */
- public static String uploadFile(String url, File file) throws IOException {
- URL urlGet = new URL(url);
- HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection();
- if (conn instanceof HttpsURLConnection) {
- conn = newHttpsURLConnection(conn);
- }
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", DEFAULT_USER_AGENT);
- //conn.setRequestProperty("Charsert", "UTF-8");
- conn.setRequestProperty("Accept-Charset", "UTF-8");
- // 定义数据分隔线
- String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL";
- conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
- OutputStream out = new DataOutputStream(conn.getOutputStream());
- // 定义最后数据分隔线
- StringBuilder mediaData = new StringBuilder();
- mediaData.append("--").append(BOUNDARY).append("\r\n");
- mediaData.append("Content-Disposition: form-data;name=\"media\";filename=\"" + file.getName() + "\"\r\n");
- mediaData.append("Content-Type:application/octet-stream\r\n\r\n");
- byte[] mediaDatas = mediaData.toString().getBytes();
- out.write(mediaDatas);
- DataInputStream fs = new DataInputStream(new FileInputStream(file));
- int bytes = 0;
- byte[] bufferOut = new byte[1024];
- while ((bytes = fs.read(bufferOut)) != -1) {
- out.write(bufferOut, 0, bytes);
- }
- closeQuietly(fs);
- // 多个文件时,二个文件之间加入这个
- //out.write("\r\n".getBytes());
- byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
- out.write(end_data);
- out.flush();
- closeQuietly(out);
- // 定义BufferedReader输入流来读取URL的响应
- InputStream in = conn.getInputStream();
- BufferedReader read = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
- String valueString = null;
- StringBuffer bufferRes = null;
- bufferRes = new StringBuffer();
- while ((valueString = read.readLine()) != null) {
- bufferRes.append(valueString);
- }
- read.close();
- closeQuietly(in);
- // 关闭连接
- if (conn != null) {
- conn.disconnect();
- }
- return bufferRes.toString();
- }
- protected static HttpsURLConnection newHttpsURLConnection(HttpURLConnection cn) {
- try {
- HttpsURLConnection conn = (HttpsURLConnection) cn;
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[] { new X509TrustManager() {
- @Override
- public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
- }
- @Override
- public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
- }
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[] {};
- }
- } }, new SecureRandom());
- conn.setSSLSocketFactory(sc.getSocketFactory());
- conn.setHostnameVerifier(new HostnameVerifier() {
- @Override
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- });
- return conn;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static void closeQuietly(Closeable closeable) {
- try {
- if (closeable != null) {
- closeable.close();
- }
- } catch (IOException ioe) {
- // ignore
- }
- }
- public static String get(String url) {
- return HttpKit.get(url);
- }
- public static String post(String url, Object data) {
- Map<String, String> headers = new HashMap<>();
- headers.put("Content-Type", "application/json");
- return HttpKit.post(url, JsonKit.toJson(data), headers);
- }
- }
类名字自己起吧, 可以叫 MyHttpKit之类的,自己改下
使用简单:
- String uploadFile(String url, File file)
- String get(String url)
- String post(String url, Object data) 《是JSON提交的
进来了就点个赞吧2333