I use httpClient interface test process, encountered the file upload interface, the previous article has completed the binary stream upload picture code, but has not encapsulated into a fixed method of use, today to share the encapsulated method, for your reference.
/** * set binary stream entities, The params parameter is "file * * @param httpPost * httpPsot request * @param params * request parameter * @param file */ public void"setMultipartEntityEntity(HttpPost httpPost, JSONObject params, File file) { String fileName = getFileName(file); InputStream inputStream = null; try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } Iterator<String> keys = params.keys(); / / traverse params MultipartEntityBuilder builder = MultipartEntityBuilder parameters and values. The create (); // Create a New Builder objectwhile (keys.hasNext()) {
String key = keys.next();
String value = params.getString(key);
if (value.equals("file")) {
builder.addBinaryBody(key, inputStream, ContentType.create("multipart/form-data"), fileName); // Set stream parameters}else {
StringBody body = new StringBody(value, ContentType.create("text/plain", Consts.UTF_8)); // Set the normal parameter Builder.addPart (key, body); } } HttpEntity entity = builder.build(); // generate entity httpPost.setentity (entity); // set entity}Copy the code
This method is only for Linux, because Windows uses “\” in the file path, in the code is “\”, so Windows friends should pay attention.
Technical articles selected
- Java one line of code to print a heart
- Linux performance monitoring software Netdata Chinese version
- Interface Test Code Coverage (JACOCO) solution sharing
- Performance testing framework
- How to perform performance tests on a Linux command line interface
- Graphic HTTP brain map
- Automatically turn the Swagger document into test code
- Five lines of code to build a static blog
- Probe into the Linear interface testing framework based on Java
- Python Plotly encapsulates methods that process interface performance test data
Selected non-technical articles
- Why software testing as a career path?
- Programming thinking for everyone
- Barriers to automated testing
- The problem with automated testing
- 7 Steps to becoming a Good Automation Test Engineer
- New trends in software testing in the next 10 years
- New trends in software testing in the next 10 years
- 17 Common Productivity Skills for Software Testers – Up
- 17 Productivity Skills used by Software testers – below
Higher-ups elegant demeanour
- Tcloud cloud measurement platform — the integrator
- Android App test tools and knowledge collection
- 4399AT UI automation CI with CD
- Android App routine test content