pom.xml:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
发送端:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
/**
* <dependency>
* <groupId>org.apache.httpcomponents</groupId>
* <artifactId>httpclient</artifactId>
* <version>4.3.6</version>
* </dependency>
* <dependency>
* <groupId>org.apache.httpcomponents</groupId>
* <artifactId>httpmime</artifactId>
* <version>4.5</version>
* </dependency>
* <dependency>
* <groupId>org.apache.httpcomponents</groupId>
* <artifactId>httpcore</artifactId>
* <version>4.4.1</version>
* </dependency>
*/
public class PostTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://10.48.47.131:9091/toolCenter/attach");// 请求地址
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
ContentType strContent = ContentType.create("application/zip", Charset.forName("UTF-8"));
meb.addBinaryBody("uploadfile", new File("D://文件.zip"));// 文件路径
meb.addTextBody("fileName", "文件.zip", strContent);// 文件名
meb.addTextBody("sendMailName", "主标题", strContent);
meb.addTextBody("mailSubject", "子标题", strContent);
meb.addTextBody("receiveMail", "anyf@kunyitech.com", strContent);//多个以;分割
meb.addTextBody("mailContent", "邮件内容", strContent);
HttpEntity httpEntity = meb.build();
httpPost.setEntity(httpEntity);
try {
StringBuilder sb = new StringBuilder();
String line;
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
while ((line = br.readLine()) != null) {
sb.append(line);
}
String rspMsg = URLDecoder.decode(sb.toString(),"UTF-8");
System.err.println(rspMsg); // {"resp_desc":"SUCCESS","resp_code":"0000"}
} catch (Exception e) {
e.printStackTrace();
}
}
}
接收端:
@RequestMapping(value = "/emailattach", produces = "application/json; charset=utf-8")
@ResponseBody
public String email(
@RequestParam("uploadfile")MultipartFile uploadfile,
@RequestParam("param1")String param1,
@RequestParam("param2")String param2
) {
log.info("receive_param1-{},param2:{}",param1,param2);
try {
String filePath = "/app/" + filename;
File file = new File(filePath);
uploadfile.transferTo(file );
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return "";
}