88import org .slf4j .Logger ;
99import org .slf4j .LoggerFactory ;
1010
11+ import javax .activation .MimetypesFileTypeMap ;
1112import javax .net .ssl .*;
12- import java .io .IOException ;
13- import java .io .InputStream ;
14- import java .io .InputStreamReader ;
15- import java .io .OutputStream ;
13+ import java .io .*;
1614import java .net .*;
1715import java .security .cert .CertificateException ;
1816import java .security .cert .X509Certificate ;
1917import java .text .MessageFormat ;
18+ import java .util .Iterator ;
19+ import java .util .Map ;
2020
2121/**
2222 * The implementation has no connection pool mechanism, used origin java connection.
@@ -283,7 +283,7 @@ private ResponseWrapper _doRequest(String url, String content,
283283 }
284284
285285 protected void initSSL (String sslVer ) {
286- TrustManager [] tmCerts = new javax . net . ssl . TrustManager [1 ];
286+ TrustManager [] tmCerts = new TrustManager [1 ];
287287 tmCerts [0 ] = new SimpleTrustManager ();
288288 try {
289289 SSLContext sslContext = SSLContext .getInstance (sslVer );
@@ -297,6 +297,118 @@ protected void initSSL(String sslVer) {
297297 }
298298 }
299299
300+ public String formUpload (String urlStr , Map <String , String > textMap ,
301+ Map <String , String > fileMap , String contentType ) {
302+ String res = "" ;
303+ HttpURLConnection conn = null ;
304+ // boundary就是request头和上传文件内容的分隔符
305+ String BOUNDARY = "---------------------------" + System .currentTimeMillis ();
306+ try {
307+ URL url = new URL (urlStr );
308+ conn = (HttpURLConnection ) url .openConnection ();
309+ conn .setConnectTimeout (5000 );
310+ conn .setReadTimeout (30000 );
311+ conn .setDoOutput (true );
312+ conn .setDoInput (true );
313+ conn .setUseCaches (false );
314+ conn .setRequestMethod ("POST" );
315+ conn .setRequestProperty ("Connection" , "Keep-Alive" );
316+ conn .setRequestProperty ("Authorization" , _authCode );
317+ conn .setRequestProperty ("Content-Type" , "multipart/form-data; boundary=" + BOUNDARY );
318+ OutputStream out = new DataOutputStream (conn .getOutputStream ());
319+ // text
320+ if (textMap != null ) {
321+ StringBuffer strBuf = new StringBuffer ();
322+ Iterator iter = textMap .entrySet ().iterator ();
323+ while (iter .hasNext ()) {
324+ Map .Entry entry = (Map .Entry ) iter .next ();
325+ String inputName = (String ) entry .getKey ();
326+ String inputValue = (String ) entry .getValue ();
327+ if (inputValue == null ) {
328+ continue ;
329+ }
330+ strBuf .append ("\r \n " ).append ("--" ).append (BOUNDARY ).append ("\r \n " );
331+ strBuf .append ("Content-Disposition: form-data; name=\" " + inputName + "\" \r \n \r \n " );
332+ strBuf .append (inputValue );
333+ }
334+ out .write (strBuf .toString ().getBytes ());
335+ }
336+ // file
337+ if (fileMap != null ) {
338+ Iterator iter = fileMap .entrySet ().iterator ();
339+ while (iter .hasNext ()) {
340+ Map .Entry entry = (Map .Entry ) iter .next ();
341+ String inputName = (String ) entry .getKey ();
342+ String inputValue = (String ) entry .getValue ();
343+ if (inputValue == null ) {
344+ continue ;
345+ }
346+ File file = new File (inputValue );
347+ String filename = file .getName ();
348+
349+ //没有传入文件类型,同时根据文件获取不到类型,默认采用application/octet-stream
350+ contentType = new MimetypesFileTypeMap ().getContentType (file );
351+ //contentType非空采用filename匹配默认的图片类型
352+ if (!"" .equals (contentType )) {
353+ if (filename .endsWith (".png" )) {
354+ contentType = "image/png" ;
355+ } else if (filename .endsWith (".jpg" ) || filename .endsWith (".jpeg" ) || filename .endsWith (".jpe" )) {
356+ contentType = "image/jpeg" ;
357+ } else if (filename .endsWith (".gif" )) {
358+ contentType = "image/gif" ;
359+ } else if (filename .endsWith (".ico" )) {
360+ contentType = "image/image/x-icon" ;
361+ }
362+ }
363+ if (contentType == null || "" .equals (contentType )) {
364+ contentType = "application/octet-stream" ;
365+ }
366+ StringBuffer strBuf = new StringBuffer ();
367+ strBuf .append ("\r \n " ).append ("--" ).append (BOUNDARY ).append ("\r \n " );
368+ strBuf .append ("Content-Disposition: form-data; name=\" " + inputName + "\" ; filename=\" " + filename + "\" \r \n " );
369+ strBuf .append ("Content-Type:" + contentType + "\r \n \r \n " );
370+ out .write (strBuf .toString ().getBytes ());
371+ DataInputStream in = new DataInputStream (new FileInputStream (file ));
372+ int bytes = 0 ;
373+ byte [] bufferOut = new byte [1024 ];
374+ while ((bytes = in .read (bufferOut )) != -1 ) {
375+ out .write (bufferOut , 0 , bytes );
376+ }
377+ in .close ();
378+ }
379+ }
380+ byte [] endData = ("\r \n --" + BOUNDARY + "--\r \n " ).getBytes ();
381+ out .write (endData );
382+ out .flush ();
383+ out .close ();
384+ // 读取返回数据
385+ StringBuffer strBuf = new StringBuffer ();
386+
387+ InputStream is = null ;
388+ int responseCode = conn .getResponseCode ();
389+ BufferedReader reader ;
390+ if (responseCode == 200 ) {
391+ reader = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
392+ } else {
393+ reader = new BufferedReader (new InputStreamReader (conn .getErrorStream ()));
394+ }
395+ String line = null ;
396+ while ((line = reader .readLine ()) != null ) {
397+ strBuf .append (line ).append ("\n " );
398+ }
399+ res = strBuf .toString ();
400+ reader .close ();
401+ reader = null ;
402+ } catch (Exception e ) {
403+ LOG .error ("formUpload error" , e );
404+ } finally {
405+ if (conn != null ) {
406+ conn .disconnect ();
407+ conn = null ;
408+ }
409+ }
410+ return res ;
411+ }
300412
301413 private static class SimpleHostnameVerifier implements HostnameVerifier {
302414
@@ -323,7 +435,7 @@ public X509Certificate[] getAcceptedIssuers() {
323435 }
324436 }
325437
326- public static class SimpleProxyAuthenticator extends java . net . Authenticator {
438+ public static class SimpleProxyAuthenticator extends Authenticator {
327439 private String username ;
328440 private String password ;
329441
0 commit comments