首先,创建servlet,如下:
package com.apache.https.sinosoft.servlet;
import com.apache.https.sinosoft.common.FTPUtils;
import com.apache.https.sinosoft.common.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@WebServlet(urlPatterns = "/ZBFReconciliationServlet")
public class ZBFReconciliationServlet extends HttpServlet {
private static Logger logger = LoggerFactory.getLogger(ZBFReconciliationServlet.class);
private static final long serialVersionUID = 1L;
public ZBFReconciliationServlet() throws IOException {
super();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("My SpringBoot Servlet");
response.getWriter().flush();
response.getWriter().close();
}
/**
* 下载对账文件至本地服务器——中保付
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("下载对账文件开始---->begin");
System.out.println("=================="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"=====================");
request.setCharacterEncoding("utf-8");
String date = request.getParameter("date");
String ftpIP = SysConfig.getPropertity("ZBFFTPIP"); // ip
String ftpPort = SysConfig.getPropertity("ZBFFTPPORT"); // port端口
String ftpUserName = SysConfig.getPropertity("ZBFFTPUSERNAME"); // 用户名
String ftpPassWord = SysConfig.getPropertity("ZBFFTPPASSWORD"); // 密码
String ftpPath = SysConfig.getPropertity("ZBFFTPPATH");// ftp路径
String localFilePath = SysConfig.getPropertity("RECONCILIATIONFILEPATH"); // 本地路径
// 从sftp下载路径
String path = ftpPath;
FTPUtils sftp = new FTPUtils();
try {
// 链接sftp进行对账文件下载
sftp.connect(ftpIP,ftpPort,ftpUserName,ftpPassWord);
List<String> strings = sftp.batchDownLoadFile(path+date+"/", localFilePath+date+"/", "", ".txt", false);
} catch (Exception e) {
e.printStackTrace();
} finally {
sftp.disconnect();
response.flushBuffer();
}
}
}
注意,一定要在springboot的启动类上加上servlet的扫描注解,不然调用会报错404,如下:
@SpringBootApplication
@ServletComponentScan("com.apache.https.sinosoft.servlet")
public class HttpsApplication {
public static void main(String[] args) {
SpringApplication.run(HttpsApplication.class, args);
}
}
接下来,就是创建http调用类了,如下:
public void uploadFileForZBF(String date){
logger.info("uploadFileForZBF--->"+date);
String dateString = date.replace("-","").replace("-","");
String requestJSON = ""; // 请求JSON串
String responseJSON = ""; // 返回JSON串
try{
// 调用http模块,链接sftp进行文件下载
String reqParamater = "?date="+dateString;
URL getUrl = new URL(ZBFUploadFileUrl + reqParamater);
HttpURLConnection httpURLConnection = (HttpURLConnection) getUrl.openConnection();
httpURLConnection.setRequestMethod("POST");
byte[] bytes = requestJSON.getBytes("UTF-8");
httpURLConnection.setRequestProperty("Content-Type","application/json");
httpURLConnection.setRequestProperty("Charset","UTF-8");
httpURLConnection.setUseCaches(false); // 请求不使用缓存
httpURLConnection.setConnectTimeout(600000);// 链接超时时间
httpURLConnection.setReadTimeout(600000);//设置从主机读取数据超时时间(毫秒)
httpURLConnection.setDoInput(true);//设置是否从httpUrlConnection读入,默认情况下是true;指示应用程序要从URL连接读取数据
httpURLConnection.setDoOutput(true);// 设置是否向httpUrlConnection输出// )
OutputStream outxml = httpURLConnection.getOutputStream();
// 发送数据
outxml.write(bytes, 0, bytes.length);
outxml.flush();
if(outxml !=null ){
outxml.close();
}
InputStream in = httpURLConnection.getInputStream();
// 取返回值
StringBuilder buffer = new StringBuilder();
int k = 0;
while (k != -1) {
k = in.read();
if (k != -1) {buffer.append((char) k);
}
}
responseJSON = new String(buffer.toString().getBytes("iso-8859-1"),"UTF-8");
System.out.println("接收json串:"+responseJSON);
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
我实现的是远程下载,大家也可以试试其他的调用实现。