Java 发送http请求上传文件功能实例
废话不多说了,直接给大家贴代码了,具体代码如下所示:
packagewxapi.WxHelper; importjava.io.BufferedReader; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.File; importjava.io.FileInputStream; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.OutputStream; importjava.net.HttpURLConnection; importjava.net.URL; importjava.net.URLConnection; importjava.util.Date; importjava.util.Map; importjava.util.Map.Entry; publicclassHttpRequestUtil{ /** *发送get请求 * *@paramrequestUrl *请求url *@paramrequestHeader *请求头 *@paramresponseEncoding *响应编码 *@return页面响应html */ publicstaticStringsendGet(StringrequestUrl,MaprequestHeader,StringresponseEncoding){ Stringresult=""; BufferedReaderreader=null; try{ if(requestUrl==null||requestUrl.isEmpty()){ returnresult; } URLrealUrl=newURL(requestUrl); URLConnectionconnection=realUrl.openConnection(); connection.setRequestProperty("accept","text/html,application/xhtml+xml,image/jxr,*/*"); connection.setRequestProperty("user-agent","Mozilla/5.0(WindowsNT10.0;WOW64;rv:53.0)Gecko/20100101Firefox/53.0"); if(requestHeader!=null&&requestHeader.size()>0){ for(Entry entry:requestHeader.entrySet()){ connection.setRequestProperty(entry.getKey(),entry.getValue()); } } connection.connect(); if(responseEncoding==null||responseEncoding.isEmpty()){ responseEncoding="UTF-8"; } reader=newBufferedReader(newInputStreamReader(connection.getInputStream(),responseEncoding)); Stringline; while((line=reader.readLine())!=null){ result+=line; } }catch(Exceptione){ System.out.println("发送GET请求出现异常!"); e.printStackTrace(); }finally{ try{ if(reader!=null){ reader.close(); } }catch(Exceptione){ e.printStackTrace(); } } returnresult; } /** *发送post请求 * *@paramrequestUrl *请求url *@paramrequestHeader *请求头 *@paramformTexts *表单数据 *@paramfiles *上传文件 *@paramrequestEncoding *请求编码 *@paramresponseEncoding *响应编码 *@return页面响应html */ publicstaticStringsendPost(StringrequestUrl,Map requestHeader,Map formTexts,Map files,StringrequestEncoding,StringresponseEncoding){ OutputStreamout=null; BufferedReaderreader=null; Stringresult=""; try{ if(requestUrl==null||requestUrl.isEmpty()){ returnresult; } URLrealUrl=newURL(requestUrl); HttpURLConnectionconnection=(HttpURLConnection)realUrl.openConnection(); connection.setRequestProperty("accept","text/html,application/xhtml+xml,image/jxr,*/*"); connection.setRequestProperty("user-agent","Mozilla/5.0(WindowsNT10.0;WOW64;rv:53.0)Gecko/20100101Firefox/53.0"); if(requestHeader!=null&&requestHeader.size()>0){ for(Entry entry:requestHeader.entrySet()){ connection.setRequestProperty(entry.getKey(),entry.getValue()); } } connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); if(requestEncoding==null||requestEncoding.isEmpty()){ requestEncoding="UTF-8"; } if(responseEncoding==null||responseEncoding.isEmpty()){ responseEncoding="UTF-8"; } if(requestHeader!=null&&requestHeader.size()>0){ for(Entry entry:requestHeader.entrySet()){ connection.setRequestProperty(entry.getKey(),entry.getValue()); } } if(files==null||files.size()==0){ connection.setRequestProperty("content-type","application/x-www-form-urlencoded"); out=newDataOutputStream(connection.getOutputStream()); if(formTexts!=null&&formTexts.size()>0){ StringformData=""; for(Entry entry:formTexts.entrySet()){ formData+=entry.getKey()+"="+entry.getValue()+"&"; } formData=formData.substring(0,formData.length()-1); out.write(formData.toString().getBytes(requestEncoding)); } }else{ Stringboundary="-----------------------------"+String.valueOf(newDate().getTime()); connection.setRequestProperty("content-type","multipart/form-data;boundary="+boundary); out=newDataOutputStream(connection.getOutputStream()); if(formTexts!=null&&formTexts.size()>0){ StringBuildersbFormData=newStringBuilder(); for(Entry entry:formTexts.entrySet()){ sbFormData.append("--"+boundary+"\r\n"); sbFormData.append("Content-Disposition:form-data;name=\""+entry.getKey()+"\"\r\n\r\n"); sbFormData.append(entry.getValue()+"\r\n"); } out.write(sbFormData.toString().getBytes(requestEncoding)); } for(Entry entry:files.entrySet()){ StringfileName=entry.getKey(); StringfilePath=entry.getValue(); if(fileName==null||fileName.isEmpty()||filePath==null||filePath.isEmpty()){ continue; } Filefile=newFile(filePath); if(!file.exists()){ continue; } out.write(("--"+boundary+"\r\n").getBytes(requestEncoding)); out.write(("Content-Disposition:form-data;name=\""+fileName+"\";filename=\""+file.getName()+"\"\r\n").getBytes(requestEncoding)); out.write(("Content-Type:application/x-msdownload\r\n\r\n").getBytes(requestEncoding)); DataInputStreamin=newDataInputStream(newFileInputStream(file)); intbytes=0; byte[]bufferOut=newbyte[1024]; while((bytes=in.read(bufferOut))!=-1){ out.write(bufferOut,0,bytes); } in.close(); out.write(("\r\n").getBytes(requestEncoding)); } out.write(("--"+boundary+"--").getBytes(requestEncoding)); } out.flush(); out.close(); out=null; reader=newBufferedReader(newInputStreamReader(connection.getInputStream(),responseEncoding)); Stringline; while((line=reader.readLine())!=null){ result+=line; } }catch(Exceptione){ System.out.println("发送POST请求出现异常!"); e.printStackTrace(); }finally{ try{ if(out!=null){ out.close(); } if(reader!=null){ reader.close(); } }catch(IOExceptionex){ ex.printStackTrace(); } } returnresult; } }
以上所述是小编给大家介绍的Java发送http请求上传文件功能实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!