Java 接口本地测试

本文介绍了在IDEA中进行Java接口本地测试的步骤,包括如何创建测试类,编写测试方法,以及如何查看测试覆盖率。通过快捷键ctrl+shift+t创建测试类,使用注解标识,然后编写测试用例,运行并查看覆盖率报告,以便于提升代码质量。
摘要由CSDN通过智能技术生成
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java本地测试文件上传的工具类示例: ```java import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { private static final String LINE_FEED = "\r\n"; private final String boundary; private HttpURLConnection httpConn; private final String charset; public FileUploader(String requestURL, String charset) throws IOException { this.charset = charset; // creates a unique boundary based on time stamp boundary = "===" + System.currentTimeMillis() + "==="; URL url = new URL(requestURL); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setUseCaches(false); httpConn.setDoOutput(true); // indicates that we want to write to the HTTP request body httpConn.setDoInput(true); httpConn.setRequestMethod("POST"); httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); } public void addFilePart(String fieldName, File uploadFile) throws IOException { String fileName = uploadFile.getName(); httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); StringBuilder sb = new StringBuilder(); sb.append("--").append(boundary).append(LINE_FEED); sb.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"" + LINE_FEED); sb.append("Content-Type: " + "application/octet-stream" + LINE_FEED); sb.append(LINE_FEED); byte[] headerBytes = sb.toString().getBytes(charset); byte[] trailerBytes = (LINE_FEED + "--" + boundary + "--" + LINE_FEED).getBytes(charset); httpConn.setRequestProperty("Content-Length", String.valueOf(headerBytes.length + uploadFile.length() + trailerBytes.length)); httpConn.getOutputStream().write(headerBytes); try (InputStream inputStream = uploadFile.toURI().toURL().openStream()) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { httpConn.getOutputStream().write(buffer, 0, bytesRead); } httpConn.getOutputStream().write(trailerBytes); } } public int finish() throws IOException { int status = httpConn.getResponseCode(); httpConn.disconnect(); return status; } } ``` 使用示例: ```java public static void main(String[] args) throws IOException { String charset = "UTF-8"; String requestURL = "http://localhost:8080/upload"; FileUploader fileUploader = new FileUploader(requestURL, charset); File uploadFile = new File("path/to/local/file"); fileUploader.addFilePart("file", uploadFile); int status = fileUploader.finish(); System.out.println("Server response code: " + status); } ``` 其中,`requestURL`是文件上传接口的URL,`uploadFile`是要上传的本地文件。你需要将它们替换为你自己的URL和文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值