http/https协议使用jsoup文件上传
发布时间:2020-12-15 03:13:26 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 依赖jsoup-1.8.2.jar ???????????????dependency ??????groupIdorg.jsoup/groupId ??????artifactIdjsoup/artifactId ??????version1.8.2/version ???
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
依赖jsoup-1.8.2.jar
???????????????<dependency> ??????<groupId>org.jsoup</groupId> ??????<artifactId>jsoup</artifactId> ??????<version>1.8.2</version> ????</dependency> [Java]代码????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(); } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |