Htttpclien循环自动生成图片,同时发送参数和文件,模拟http的post请求
package org.jeecg.modules.bussiness.PostTests;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import javax.imageio.ImageIO;
public class Myhttpclient
{
public static void main(String args[]) throws IOException, InterruptedException {
int i=0;
while(true) {
String targetURL = null;// TODO 指定URL
File targetFile = null;// TODO 指定上传文件
targetFile = createImage(i);
i++;
long strParam = System.currentTimeMillis();
String strJson = "{"id":"" +
strParam +
"","createBy":"10001","createTime":"3021-10-15 14:45:41","updateBy":"10001","updateTime":"3021-10-15 14:45:41","sysOrgCode":"10001","securDoorNum":"10001","securDoorStatus":"606","bodyTemperature":"11","metalTestResult":"12","abnormalTemRecord":"13","faceDetectionResults":"44","dateTime":"3021-10-15 14:45:41","realTime":"3021-10-15 14:45:41"}";
targetURL = "http://localhost:8080/jeecg-boot/bussiness/sopManagementInfo/addR";
PostMethod filePost = new PostMethod(targetURL);
filePost.setRequestHeader("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9." +
"eyJleHAiOjE2MzcwMjk2NzQsInVzZXJuYW1lIjoiYWRtaW4ifQ.qXDwEDRItZgRZ-oEsfQXPIGjuimM9LWj9z4Wcmguyrc");
try {
Part[] parts = {
new FilePart("myfile", targetFile),
new StringPart("jsonstr", strJson)
};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
System.out.println("上传成功");
} else {
System.out.println("上传失败");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
filePost.releaseConnection();
}
}
}
static File createImage(int i) throws IOException, InterruptedException {
int width = 1000;
int height = 1000;
String s=create();
File file = new File("D:/testImage/image"+i+".jpg");
Font font = new Font("宋体", Font.PLAIN, 150);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.black);
g2.clearRect(0, 0, width, height);
g2.setPaint(Color.RED);
g2.setFont(font);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(s, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
g2.drawString(s, (int)x, (int)baseY);
ImageIO.write(bi, "jpg", file);
Thread.sleep(1500);
return file;
}
static String create(){
String s="";
Random r = new Random();
for(int i=0 ; i<5 ; i++)
{
int ran1 = r.nextInt(1000);
s+=ran1;
}
return s;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)