工具类静态常量
private final static String BOUNDARY = UUID . randomUUID ( ) . toString ( ) . toLowerCase ( ) . replaceAll ( "-" , "" ) ;
private final static String PREFIX = "--" ;
private final static String LINE_END = "\r\n" ;
主方法
public static String sendRequest ( String requestUrl, Map< String, String> requestText, Map< String, File> requestFile) throws Exception{
HttpURLConnection conn = null ;
InputStream input = null ;
OutputStream os = null ;
BufferedReader br = null ;
StringBuffer buffer = null ;
try {
URL url = new URL ( requestUrl) ;
conn = ( HttpURLConnection) url. openConnection ( ) ;
conn. setDoOutput ( true ) ;
conn. setDoInput ( true ) ;
conn. setUseCaches ( false ) ;
conn. setConnectTimeout ( 1000 * 10 ) ;
conn. setReadTimeout ( 1000 * 30 ) ;
conn. setRequestMethod ( "POST" ) ;
conn. setRequestProperty ( "Accept" , "*/*" ) ;
conn. setRequestProperty ( "Connection" , "keep-alive" ) ;
conn. setRequestProperty ( "User-Agent" , "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)" ) ;
conn. setRequestProperty ( "Charset" , "UTF-8" ) ;
conn. setRequestProperty ( "Content-Type" , "multipart/form-data; boundary=" + BOUNDARY ) ;
conn. connect ( ) ;
os = new DataOutputStream ( conn. getOutputStream ( ) ) ;
writeParams ( requestText, os) ;
writeFile ( requestFile, os) ;
String endTarget = PREFIX + BOUNDARY + PREFIX + LINE_END ;
os. write ( endTarget. getBytes ( ) ) ;
os. flush ( ) ;
if ( conn. getResponseCode ( ) == 200 ) {
input = conn. getInputStream ( ) ;
} else {
input = conn. getErrorStream ( ) ;
}
br = new BufferedReader ( new InputStreamReader ( input, "UTF-8" ) ) ;
buffer = new StringBuffer ( ) ;
String line = null ;
while ( ( line = br. readLine ( ) ) != null ) {
buffer. append ( line) ;
}
} catch ( Exception e) {
log. error ( "发送请求错误" + e. getMessage ( ) ) ;
throw new Exception ( e) ;
} finally {
try {
if ( conn != null ) {
conn. disconnect ( ) ;
conn = null ;
}
if ( os != null ) {
os. close ( ) ;
os = null ;
}
if ( br != null ) {
br. close ( ) ;
br = null ;
}
} catch ( IOException ex) {
throw new Exception ( ex) ;
}
}
return buffer. toString ( ) ;
}
其他方法
private static void writeParams ( Map< String, String> requestText,
OutputStream os) throws Exception {
try {
StringBuilder requestParams = new StringBuilder ( ) ;
Set< Map. Entry< String, String>> set = requestText. entrySet ( ) ;
Iterator< Entry< String, String>> it = set. iterator ( ) ;
while ( it. hasNext ( ) ) {
Entry< String, String> entry = it. next ( ) ;
requestParams. append ( PREFIX ) . append ( BOUNDARY ) . append ( LINE_END ) ;
requestParams. append ( "Content-Disposition: form-data; name=\"" )
. append ( entry. getKey ( ) ) . append ( "\"" ) . append ( LINE_END ) ;
requestParams. append ( "Content-Type: text/plain; charset=utf-8" )
. append ( LINE_END ) ;
requestParams. append ( "Content-Transfer-Encoding: 8bit" ) . append (
LINE_END ) ;
requestParams. append ( LINE_END ) ;
requestParams. append ( entry. getValue ( ) ) ;
requestParams. append ( LINE_END ) ;
}
os. write ( requestParams. toString ( ) . getBytes ( ) ) ;
os. flush ( ) ;
} catch ( Exception e) {
throw new Exception ( e) ;
}
}
private static void writeFile ( Map< String, File> requestFile,
OutputStream os) throws Exception {
InputStream is = null ;
try {
if ( requestFile == null || requestFile. isEmpty ( ) ) {
return ;
} else {
StringBuilder requestParams = new StringBuilder ( ) ;
Set< Map. Entry< String, File>> set = requestFile. entrySet ( ) ;
Iterator< Entry< String, File>> it = set. iterator ( ) ;
while ( it. hasNext ( ) ) {
Entry< String, File> entry = it. next ( ) ;
requestParams. append ( PREFIX ) . append ( BOUNDARY ) . append ( LINE_END ) ;
requestParams. append ( "Content-Disposition: form-data; name=\"" )
. append ( entry. getKey ( ) ) . append ( "\"; filename=\"" )
. append ( entry. getValue ( ) . getName ( ) ) . append ( "\"" )
. append ( LINE_END ) ;
requestParams. append ( "Content-Type:" )
. append ( getContentType ( entry. getValue ( ) ) )
. append ( LINE_END ) ;
requestParams. append ( "Content-Transfer-Encoding: 8bit" ) . append (
LINE_END ) ;
requestParams. append ( LINE_END ) ;
os. write ( requestParams. toString ( ) . getBytes ( ) ) ;
is = new FileInputStream ( entry. getValue ( ) ) ;
byte[ ] buffer = new byte [ 1024 * 1024 ] ;
int len = 0 ;
while ( ( len = is. read ( buffer) ) != - 1 ) {
os. write ( buffer, 0 , len) ;
}
os. write ( LINE_END . getBytes ( ) ) ;
os. flush ( ) ;
}
}
} catch ( Exception e) {
throw new Exception ( e) ;
} finally {
try {
if ( is!= null ) {
is. close ( ) ;
}
} catch ( Exception e) {
throw new Exception ( e) ;
}
}
}
public static String getContentType ( File file) throws Exception{
String streamContentType = "application/octet-stream" ;
String fileNameTmp = file. getName ( ) . toLowerCase ( Locale. ENGLISH ) ;
if ( fileNameTmp. endsWith ( "csv" ) ) {
return "text/csv" ;
}
if ( fileNameTmp. endsWith ( "txt" ) ) {
return "text/plain" ;
}
if ( fileNameTmp. endsWith ( "gif" ) ) {
return "image/gif" ;
}
if ( fileNameTmp. endsWith ( "jpg" ) || fileNameTmp. endsWith ( "jpg" ) || fileNameTmp. endsWith ( "jpeg" ) || fileNameTmp. endsWith ( "jpe" ) ) {
return "image/jpeg" ;
}
if ( fileNameTmp. endsWith ( "zip" ) ) {
return "application/zip" ;
}
if ( fileNameTmp. endsWith ( "rar" ) ) {
return "application/rar" ;
}
if ( fileNameTmp. endsWith ( "doc" ) ) {
return "application/msword" ;
}
if ( fileNameTmp. endsWith ( "ppt" ) ) {
return "application/vnd.ms-powerpoint" ;
}
if ( fileNameTmp. endsWith ( "xls" ) ) {
return "application/vnd.ms-excel" ;
}
if ( fileNameTmp. endsWith ( "html" ) || fileNameTmp. endsWith ( "htm" ) ) {
return "text/html" ;
}
if ( fileNameTmp. endsWith ( "tif" ) || fileNameTmp. endsWith ( "tiff" ) ) {
return "image/tiff" ;
}
if ( fileNameTmp. endsWith ( "pdf" ) ) {
return "application/pdf" ;
}
return streamContentType;
}
调用示例
public String sendFileToGDDX ( String uploadFilePath, String uploadFileName, File uploadFile ) {
String result = "" ;
String uploadUrl = "http://" + RoundRobin. getBankbakServer ( ) + "/queryLLWX/QueryNate.shtml?action=sendFileToSZFTP&type=%s×tamp=%s&sign=%s" ;
try {
String timestamp = System. currentTimeMillis ( ) + "" ;
String type = "sendFileToGDDX" ;
Map< String, String> check = new HashMap < > ( ) ;
check. put ( "type" , type) ;
check. put ( "timestamp" , timestamp) ;
String sign = AESUtil. encrypt ( AESUtil. getSortParam ( check) ) ;
uploadUrl = String. format ( uploadUrl, type, timestamp, sign) ;
Map< String, String> requestMap = new HashMap < > ( ) ;
requestMap. put ( "uploadFileName" , uploadFileName) ;
requestMap. put ( "uploadFilePath" , "/WsyytWeChat" + uploadFilePath) ;
Map< String, File> FileMap = new HashMap < > ( ) ;
FileMap. put ( "uploadFile" , uploadFile) ;
result = MHttpsUtil. sendRequest ( uploadUrl, requestMap, FileMap) ;
} catch ( Exception e) {
result = "post File Error:" + e. getMessage ( ) ;
}
return result;
}