常用网络设备图片-遨游3浏览器

httpservletrequest
2023年4月3日发(作者:pdf电子书制作)

java实现http调⽤servlet,并且进⾏参数传递

⾸先,创建servlet,如下:

t;

ls;

fig;

;

Factory;

;

tException;

vlet;

rvlet;

rvletRequest;

rvletResponse;

ption;

DateFormat;

;

;

@WebServlet(urlPatterns="/ZBFReconciliationServlet")

publicclassZBFReconciliationServletextendsHttpServlet{

privatestaticLoggerlogger=ger();

privatestaticfinallongserialVersionUID=1L;

publicZBFReconciliationServlet()throwsIOException{

super();

}

@Override

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

ter().print("MySpringBootServlet");

ter().flush();

ter().close();

}

/**

*下载对账⽂件⾄本地服务器——中保付

*@paramrequest

*@paramresponse

*@throwsServletException

*@throwsIOException

*/

@Override

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

("下载对账⽂件开始---->begin");

n("=================="+newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(newDate())+"=====================");

racterEncoding("utf-8");

Stringdate=ameter("date");

StringftpIP=pertity("ZBFFTPIP");//ip

StringftpPort=pertity("ZBFFTPPORT");//port端⼝

StringftpUserName=pertity("ZBFFTPUSERNAME");//⽤户名

StringftpPassWord=pertity("ZBFFTPPASSWORD");//密码

StringftpPath=pertity("ZBFFTPPATH");//ftp路径

StringlocalFilePath=pertity("RECONCILIATIONFILEPATH");//本地路径

//从sftp下载路径

Stringpath=ftpPath;

FTPUtilssftp=newFTPUtils();

try{

try{

//链接sftp进⾏对账⽂件下载

t(ftpIP,ftpPort,ftpUserName,ftpPassWord);

Liststrings=ownLoadFile(path+date+"/",localFilePath+date+"/","",".txt",false);

}catch(Exceptione){

tackTrace();

}finally{

nect();

uffer();

}

}

}

注意,⼀定要在springboot的启动类上加上servlet的扫描注解,不然调⽤会报错404,如下:

@SpringBootApplication

@ServletComponentScan("t")

publicclassHttpsApplication{

publicstaticvoidmain(String[]args){

(,args);

}

}

接下来,就是创建http调⽤类了,如下:

publicvoiduploadFileForZBF(Stringdate){

("uploadFileForZBF--->"+date);

StringdateString=e("-","").replace("-","");

StringrequestJSON="";//请求JSON串

StringresponseJSON="";//返回JSON串

try{

//调⽤http模块,链接sftp进⾏⽂件下载

StringreqParamater="?date="+dateString;

URLgetUrl=newURL(ZBFUploadFileUrl+reqParamater);

HttpURLConnectionhttpURLConnection=(HttpURLConnection)nnection();

uestMethod("POST");

byte[]bytes=es("UTF-8");

uestProperty("Content-Type","application/json");

uestProperty("Charset","UTF-8");

Caches(false);//请求不使⽤缓存

nectTimeout(600000);//链接超时时间

dTimeout(600000);//设置从主机读取数据超时时间(毫秒)

nput(true);//设置是否从httpUrlConnection读⼊,默认情况下是true;指⽰应⽤程序要从URL连接读取数据

utput(true);//设置是否向httpUrlConnection输出//)

OutputStreamoutxml=putStream();

//发送数据

(bytes,0,);

();

if(outxml!=null){

();

}

InputStreamin=utStream();

//取返回值

StringBuilderbuffer=newStringBuilder();

intk=0;

while(k!=-1){

k=();

if(k!=-1){((char)k);

}

}

responseJSON=newString(ng().getBytes("iso-8859-1"),"UTF-8");

n("接收json串:"+responseJSON);

();

}catch(Exceptione){

tackTrace();

}

}

我实现的是远程下载,⼤家也可以试试其他的调⽤实现。

更多推荐

httpservletrequest