HttpUtils——OkHttp版本
已经不开发安卓了,目前HttpClient用的多,此版本留做纪念
cn.seaboot.common是我的工具包,可用apache的工具包代替
<!--暂未使用--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.14.2</version> </dependency>
package cn.seaboot.common.net; import cn.seaboot.common.core.CommonUtils; import cn.seaboot.common.core.Converter; import cn.seaboot.common.exception.ServiceException; import cn.seaboot.common.file.IOUtils; import cn.seaboot.common.file.MimeUtils; import okhttp3.*; import okio.BufferedSink; import org.apache.http.HttpStatus; import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; /** * 基于OkHttp */ public class OkHttpUtils { private static OkHttpClient okHttpClient; private OkHttpUtils() { okHttpClient = new OkHttpClient(); } /** * 异常校验 */ private static ResponseBody validResponse(Response res) { if (res.code() != HttpStatus.SC_OK) { throw new ServiceException("okhttp visit url exception :" + res.code()); } return res.body(); } /** * 构建表单 */ public static FormBody getFormBody(String url, HashMap<String, ?> map) { FormBody.Builder builder = new FormBody.Builder(); String v; for (Map.Entry<String, ?> p : map.entrySet()) { v = Converter.toString(p.getValue()); builder.add(p.getKey(), v); } return builder.build(); } /** * post请求 */ public static Request getPostMethod(String url, HashMap<String, ?> map) { return new Request.Builder() .url(url) .post(getFormBody(url, map)) .build(); } public static String getUrl(String url, HashMap<String, ?> map){ StringBuilder sb = new StringBuilder(url); if (url.indexOf('?') < 0) { sb.append('?'); } String v; for (Map.Entry<String, ?> p : map.entrySet()) { v = Converter.toString(p.getValue()); sb.append(p.getKey()).append('=').append(v).append('&'); } sb.setLength(sb.length() - 1); return sb.toString(); } /** * get请求 */ public static Request getGetMethod(String url, HashMap<String, ?> map) throws IOException { return new Request.Builder() .url(getUrl(url, map)) .get() .build(); } public static String execute(Request request) throws IOException { Call call = okHttpClient.newCall(request); return validResponse(call.execute()).string(); } public static void download(Request request, OutputStream os) throws IOException { Call call = okHttpClient.newCall(request); IOUtils.copy(validResponse(call.execute()).byteStream(), os); } public static String postFormData(String url, HashMap<String, ?> map, String name, File file) throws IOException { String v = file.getName(); String suffix = v.substring(v.lastIndexOf('.') + 1); String mime = MimeUtils.getMimeFromSuffix(suffix); //mime类型 RequestBody fileBody = RequestBody.create(MediaType.parse(mime), file); MultipartBody.Builder builder = new MultipartBody.Builder() .addFormDataPart(name, v, fileBody); if(!CommonUtils.isEmpty(map)){ for (Map.Entry<String, ?> p : map.entrySet()) { v = Converter.toString(p.getValue()); builder.addFormDataPart(p.getKey(), v); } } System.out.println(mime); Request request = new Request.Builder() .url(url) .post(builder.build()) .build(); return execute(request); } public static String postFormData(String url, Map<String, ?> map, String name, String mime, InputStream is) throws IOException { RequestBody fileBody = new RequestBody() { @Override @Nullable public MediaType contentType() { return MediaType.parse(mime); } @Override public long contentLength() { try { return is.available(); } catch (IOException e) { //IGNORE } return -1; } @Override public void writeTo(BufferedSink sink) throws IOException { IOUtils.copy(is,sink.outputStream()); } }; MultipartBody.Builder builder = new MultipartBody.Builder() .addFormDataPart(name, name, fileBody); String v; if(!CommonUtils.isEmpty(map)){ for (Map.Entry<String, ?> p : map.entrySet()) { v = Converter.toString(p.getValue()); builder.addFormDataPart(p.getKey(), v); } } Request request = new Request.Builder() .url(url) .post(builder.build()) .build(); return execute(request); } public static void main(String[] args) throws IOException { new OkHttpUtils(); String url = "http://localhost:8080/xbry/md2001/ammunCode/test"; String path = "C:\\Users\\Administrator\\Desktop\\logo_144.png"; Map<String, Object> map = new HashMap<>(); map.put("3", "4"); File file = new File(path); postFormData(url, map, "file", "image/png", IOUtils.openFileInputStream(file)); } }
疯狂的妞妞 :每一天,做什么都好,不要什么都不做!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY