java http请求
---------------------------1接口请求----------------------------------------
/**
* 定义编码格式 UTF-8
*/
public static final String URL_PARAM_DECODECHARSET_UTF8 = "UTF-8";
/**
* 定义编码格式 GBK
*/
public static final String URL_PARAM_DECODECHARSET_GBK = "GBK";
private static final String URL_PARAM_CONNECT_FLAG = "&";
private static final String EMPTY = "";
private static MultiThreadedHttpConnectionManager connectionManager = null;
private static int connectionTimeOut = 25000;
private static int socketTimeOut = 25000;
private static int maxConnectionPerHost = 20;
private static int maxTotalConnections = 20;
private static HttpClient client;
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
/**
* 执行一个HTTP POST请求,返回请求响应的HTML
*
* @param url 请求的URL地址
* @param params 请求的查询参数,能够为null
* @param charset 字符集
* @param pretty 是否美化
* @return 返回请求响应的HTML
*/
public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
//设置参数的字符集
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,charset);
//设置Http Post数据
if (params != null) {
//HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
//p.setParameter(entry.getKey(), entry.getValue());
method.setParameter(entry.getKey(), entry.getValue());
}
//method.setParams(p);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (IOException e) {
logger.error("执行HTTP Post请求" + url + "时,发生异常!", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
-----------------------2SFTP附件下载 SFTPUtil.java文件在当前根目录-----------------------------
/**
* 获取ip地址
* 1.传入参数ip地址
* 2.根据ip地址下载文件
* 3.并把文件上传到h3c文件服务器
* 4.获取新的ip
*
* @return
*/
@SneakyThrows
@Override
public Boolean uploadIpAddress() {
log.error("-----------------------附件拉取开始-----------------------");
int startId=1;
int endId=1;
//查询文件表最大id
QueryWrapper<MatFile> matFileMaxQueryWrapper = new QueryWrapper<>();
matFileMaxQueryWrapper.lambda().select(MatFile::getId);
matFileMaxQueryWrapper.lambda().orderByDesc(MatFile::getId);
matFileMaxQueryWrapper.last(" limit 1 ");
MatFile matFileMax=new MatFile().selectOne(matFileMaxQueryWrapper);
if(matFileMax!=null&&matFileMax.getId()!=null){
endId=matFileMax.getId().intValue();
}
//查询ftp表最大id
QueryWrapper<MatSftpFileMax> matSftpFileMaxQueryWrapper = new QueryWrapper<>();
matSftpFileMaxQueryWrapper.lambda().select(MatSftpFileMax::getMatFileId);
matSftpFileMaxQueryWrapper.lambda().orderByDesc(MatSftpFileMax::getMatFileId);
matSftpFileMaxQueryWrapper.last(" limit 1 ");
MatSftpFileMax matSftpFileMax=new MatSftpFileMax().selectOne(matSftpFileMaxQueryWrapper);
if(matSftpFileMax!=null&&matSftpFileMax.getMatFileId()!=null){
startId=matSftpFileMax.getMatFileId().intValue();
}
for (int j = startId; j <= endId; j++) {
System.out.println("**********开始处理编号为" + j + "的matFile表id的数据**********");
Long id = Long.parseLong(Integer.valueOf(j).toString());
QueryWrapper<MatFile> matFileQueryWrapper = new QueryWrapper<>();
matFileQueryWrapper.lambda().select(MatFile::getId, MatFile::getFileAddr, MatFile::getFileName);
matFileQueryWrapper.lambda().isNotNull(MatFile::getFileAddr);
matFileQueryWrapper.lambda().ne(MatFile::getFileAddr, "");
matFileQueryWrapper.lambda().like(MatFile::getFileAddr, "http");
matFileQueryWrapper.lambda().eq(MatFile::getId, id);
MatFile temp = new MatFile().selectOne(matFileQueryWrapper);
Calendar now = Calendar.getInstance();
//今天上次的文件夹
String nowYearMonth = now.get(Calendar.YEAR) + ((now.get(Calendar.MONTH) + 1 < 10) ? "0"
+ (now.get(Calendar.MONTH) + 1) : "" + (now.get(Calendar.MONTH) + 1))
+ ((now.get(Calendar.DAY_OF_MONTH) < 10) ? "0" + (now.get(Calendar.DAY_OF_MONTH)) : ""
+ (now.get(Calendar.DAY_OF_MONTH)));
if (temp != null) {
String fileAddr = temp.getFileAddr();
List<MatSftpFile> matSftpFiles = baseMapper.selectList(new LambdaQueryWrapper<MatSftpFile>()
.eq(MatSftpFile::getMatFileAddr, fileAddr)
.select(MatSftpFile::getId));
if (matSftpFiles == null || matSftpFiles.size() == 0) {
String content = "";
MatSftpFileLog matSftpFileLog = new MatSftpFileLog();
MatSftpFile matSftpFile = new MatSftpFile();
int i = temp.getFileName().lastIndexOf(StrUtil.DOT);
String lastFileName = temp.getFileName().substring(i + 1);
//生成文件名称
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + lastFileName;
String newFileUrl = "";
try {
//获取当前url的文件流
URL url = new URL(temp.getFileAddr());
// 获取连接
URLConnection conn = url.openConnection();
// 重新获取流
InputStream input = conn.getInputStream();
//填写服务器信息,登录服务器
SFTPUtil sftp = new SFTPUtil(sftpUserName, sftpPassword, sftpHost, sftpPort);
//sftp服务器登录
sftp.login();
//sftp服务器上传
sftp.upload(ftpPath, "/" + nowYearMonth, fileName, input);
newFileUrl = "http://" + sftpHost + ":" + sftpShowPort + "/" + nowYearMonth + "/" + fileName;
//sftp服务器推出并关闭流
sftp.logout();
input.close();
} catch (FileNotFoundException e) {
logger.error("------没有找到-----:" + fileName + "文件");
content = "没有找到当前文件";
e.printStackTrace();
} catch (SocketException e) {
logger.error("连接FTP失败.");
content = "连接FTP服务器失败";
e.printStackTrace();
} catch (IOException e) {
logger.error("文件读取错误.");
content = "文件读取失败";
e.printStackTrace();
} finally {
if (StringUtils.isNotBlank(content)) {
matSftpFileLog.setMatFileId(temp.getId());
matSftpFileLog.setMatFileName(temp.getFileName());
matSftpFileLog.setMatFileAddr(temp.getFileAddr());
matSftpFileLog.setContent(content);
matSftpFileLogService.save(matSftpFileLog);
}
}
matSftpFile.setMatFileId(temp.getId());
matSftpFile.setMatFileAddr(temp.getFileAddr());
matSftpFile.setMatFileTime(temp.getModifyTime());
matSftpFile.setFileName(temp.getFileName());
matSftpFile.setFileAddr(newFileUrl);
matSftpFile.setNfileName(fileName);
this.save(matSftpFile);
}
}
MatSftpFileMax fileMax=new MatSftpFileMax();
fileMax.setMatFileId(Long.valueOf(j));
matSftpFileMaxService.save(fileMax);
System.out.println("**********结束处理编号为" + j + "的matFile表id的数据**********");
}
log.error("-----------------------附件拉取结束-----------------------");
return Boolean.TRUE;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!