这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
整个环境由两台电脑组成,操作系统分别是 win10 和 ubuntu16,如下图:
如上图,在 win10 电脑上运行一个 java 类,发起 POST 请求将文件提交到 ubuntu 电脑上的 Docker 容器中,该容器运行着上传文件的 web 服务,在 win10 电脑上安装有 wireshark,用来分析这个上传文件的 POST 请求;
注:客户端和服务端部署在不同的机器上,这样方便 wireshark 抓包,您也可以用 vmware 在 win10 上装一个 ubuntu 虚拟机,不过此时 wireshark 抓包前请注意选择正确的网卡(vmware 虚拟出的那个);
docker run --name fileserver001 -p 8080:8080 -v /usr/local/work/fileupload/upload:/usr/Downloads -idt bolingcavalry/springbootfileserver:0.0.1-SNAPSHOT
注:有关该容器的镜像的会在下一章详细说明,本章我们就直接拿来用吧,作为上传文件的服务端;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>uploadfileclient</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.5</version>
</dependency>
</dependencies>
</project>
package com.bolingcavalry;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
/**
* @Description : 上传文件的类,将本地文件POST到server
* @Author : zq2599@gmail.com
* @Date : 2018-02-24 18:12
*/
public class UploadFileClient {
/**
* 文件服务的ULR
*/
private static final String POST_URL = "http://www.bolingcavalry.com:8088/springmvcfileserver/upload";
/**
* 要上传的本地文件的完整路径加文件名
*/
private static final String UPLOAD_FILE_FULLPATH = "D:\\temp\\201802\\21\\abc.zip";
public static void main(String[] args) throws Exception{
System.out.println("start upload");
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(POST_URL);
//基本的配置信息
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
httppost.setConfig(requestConfig);
//要上传的文件
FileBody bin = new FileBody(new File(UPLOAD_FILE_FULLPATH));
//在POST中添加一个字符串请求参数
StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", comment).build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
//发起POST
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseEntityStr = EntityUtils.toString(response.getEntity());
System.out.println("response status : " + response.getStatusLine());
System.out.println("response content length: " + resEntity.getContentLength());
System.out.println("response entity str : " + responseEntityStr);
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("end upload");
}
}
mvn clean compile -U exec:java -Dexec.mainClass="com.bolingcavalry.UploadFileClient"
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ uploadfileclient ---
start upload
executing request POST http://192.168.119.155:8080/upload HTTP/1.1
response status : HTTP/1.1 200
response content length: 40
response entity str : SpringBoot环境下,上传文件成功
end upload
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.226 s
[INFO] Finished at: 2018-02-25T00:00:13+08:00
[INFO] Final Memory: 20M/181M
[INFO] ------------------------------------------------------------------------
“SpringBoot 环境下,上传文件成功" 这一句是服务端接收文件成功后返回的信息;
2018-02-24 16:00:13.077 INFO 1 --- [nio-8080-exec-5] c.b.s.controller.UploadController : start upload, comment [This is comment]
2018-02-24 16:00:13.079 INFO 1 --- [nio-8080-exec-5] c.b.s.controller.UploadController : base save path [/tmp/tomcat-docbase.8533878395220260315.8080/WEB-INF/upload], original file name [abc.zip]
2018-02-24 16:00:13.080 INFO 1 --- [nio-8080-exec-5] c.b.s.controller.UploadController : real save path [/tmp/tomcat-docbase.8533878395220260315.8080/WEB-INF/upload/15/1], real file name [fba8a275-cfc0-4471-b4f7-21d2913450cb_abc.zip]
2018-02-24 16:00:13.080 INFO 1 --- [nio-8080-exec-5] c.b.s.controller.UploadController : save file success [/tmp/tomcat-docbase.8533878395220260315.8080/WEB-INF/upload/15/1/fba8a275-cfc0-4471-b4f7-21d2913450cb_abc.zip]
docker run --name tomcat006 -p 8088:8080 -v /usr/local/work/fileupload/upload:/usr/Downloads -idt bolingcavalry/online_deploy_tomcat:0.0.1
领取专属 10元无门槛券
私享最新 技术干货