晋级TypeScript高手,成为抢手的前端开发人才

Posted a7mddej3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了晋级TypeScript高手,成为抢手的前端开发人才相关的知识,希望对你有一定的参考价值。

downloada:晋级TypeScript高手,成为抢手的前端开发人才

依赖jsoup-1.8.2.jar

  org.jsoup
  jsoup
  1.8.2   

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import javax.net.ssl.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
/**

  • Created by Jane on 2015/9/10.
    */

public class JsoupHttpRequest {

public static void main(String[] args) throws Exception {
    String url = "http://localhost:8080/fileUpload";
    File file = new File("/opt/fileUpload/index.jpg");
    String fileRquestParam = "file";
    Map<String, String> dataMap = new HashMap<String, String>();
    dataMap.put("userName", "admin");
    dataMap.put("md5", "12cd76nskju98zud7fda0f6c9wa54");
    Response response = doPostFileRequest(url, dataMap, file, fileRquestParam);
    System.out.println(response.statusMessage());
}
/**
 * @param url              恳求的Url
 * @param paramMap         参数
 * @param file             文件
 * @param fileRequestParam form表单对应的文件name属性名
 * @return
 * @throws Exception
 */
public static Response doPostFileRequest(String url, Map<String, String> paramMap, File file, String fileRequestParam) throws Exception {
    if (StringUtils.isBlank(url)) {
        throw new Exception("The request URL is blank.");
    }
    // Https恳求
    if (StringUtils.startsWith(url, "https")) {
        trustEveryone();
    }
    Connection connection = Jsoup.connect(url);
    connection.method(Connection.Method.POST);
    connection.timeout(12000);
    connection.header("Content-Type", "multipart/form-data");
    connection.ignoreHttpErrors(true);
    connection.ignoreContentType(true);
    if (paramMap != null && !paramMap.isEmpty()) {
        connection.data(paramMap);
    }
    try {
        FileInputStream fis = new FileInputStream(file);
        connection.data(fileRequestParam, file.getName(), fis);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        Response response = connection.execute();
        if (response.statusCode() != HttpStatus.SC_OK) {
            throw new Exception("http恳求响应码:" + response.statusCode() + "");
        }
        return response;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * 处理Https恳求,返回404错误
 */
private static void trustEveryone() {
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }}, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以上是关于晋级TypeScript高手,成为抢手的前端开发人才的主要内容,如果未能解决你的问题,请参考以下文章

晋级TypeScript高手,成为抢手的前端开发人才

深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)

看直播拿证书 | 12 天,0 基础晋级 Serverless 高手

如何抢占先机,成为抢手后端开发人才! | 极客时间

看直播拿证书 | 12 天,0 基础晋级 Serverless 高手

TypeScript 源码详细解读总览