Introduce ideas

1. Use httpclent

2. Use the MultipartEntityBuilder class to pass in simulated data

3. Check the API of the MultipartEntityBuilder class to find the corresponding method to use the commit

4. Pass the text file to the object

5. Convert the object to HttpEntity

6. Call httpentity httpclient

7. Return ok

 

Without further ado, get right to the code

@suppressWarnings ("rawtypes") private String sendMail(String URL, HttpServletRequest req) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httppost = new HttpPost(url); MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); // Add text information Map Map = req.getparameterMap (); Set keSet = map.entrySet(); for (Iterator itr = keSet.iterator(); itr.hasNext();) { Map.Entry me = (Map.Entry) itr.next(); Object ok = me.getKey(); Object ov = me.getValue(); String[] value = new String[1]; if (ov instanceof String[]) { value = (String[]) ov; } else { value[0] = ov.toString(); } for (int k = 0; k < value.length; k++) { if (ok.equals("url")) continue; multipartEntity.addTextBody((String) ok, value[k]); }} the if (ServletFileUpload isMultipartContent (the req)) {/ / add files MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; Iterator<String> itr = multipartRequest.getFileNames(); while (itr.hasNext()) { String name = itr.next(); MultipartFile file = multipartRequest.getFile(name); String fileName = file.getOriginalFilename(); multipartEntity.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName); / / file stream multipartEntity. AddTextBody (" filename ", filename); // Similar to browser form submission, corresponding to input name and value}} HttpEntity reqEntity = multipartEntity. Build (); httppost.setEntity(reqEntity); HttpResponse httpResponse = httpclient.execute(httppost); HttpEntity entity = httpResponse.getEntity(); String rawHTMLContent = EntityUtils.toString(entity); EntityUtils.consume(entity); return rawHTMLContent; } finally { httpclient.close(); }}Copy the code

 

 

ok

 

 

 

Continuously updated