上传和下载的测试代码
1. 所需依赖坐标
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.1.0</version>
</dependency>
<!-- apche httpclient的maven依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
2. 测试代码
package com.hexin.test;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.hexin.protobuf.ProtoEntityUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.SQLOutput;
import java.util.List;
public class TestHttpClient1 {
@Test
public void uploadRequst() throws IOException {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("Id123","虎虎生威");
InputStreamBody inputStreamBody = new InputStreamBody((InputStream) new FileInputStream("d:/text.txt"),"utf-8");
System.out.println("builder : "+builder.toString());
System.out.println("---------开始上传----------");
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String url =
"https://cs.10jqka.com.cn/multiStorage?reqtype=upload&appname=blockstock&userid=622241656&sessionid=1cf373c85dc5d7d57ccd3e016e33be99f&expires=2022-02-23+10%3A32%3A33&token=NHDIONIEAOCBCNEKDFEALKMOEIIHJELICGIAOGGCKLIKFJMLOFIPADHBGIFNCHEEOGGHLAIMEAINGGOPMMKPIKGHFPFEILNLKKEMCEPAPOOHAPIINCPLPIOMCNAIKNBFBPLIPOKDJODMPPEDIJIPACNKBDDBIFJMDPAFEMEMNEPIFBAEDEOJMFMHOOMIJAJLGJJDKILICDALADDKLHCJBBPKGGKPOKMFDCGJPOJBDJBEHABEDHADJDMPFKKPAGJG&storepath=/&version=2";
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(builder.build());
response = httpclient.execute(httpPost);
ProtoEntityUtil.UploadResponse uploadResponse = ProtoEntityUtil.UploadResponse.parseFrom(response.getEntity().getContent());
int code =uploadResponse.getCode();
System.out.println("code : " + code);
long version = uploadResponse.getVersion();
System.out.println("version : " + version);
System.out.println("-------------上传结束-----------------");
}
@Test
public void downLoadTest() throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String url =
"https://cs.10jqka.com.cn/multiStorage?reqtype=download&appname=blockstock&userid=622241656&sessionid=1cf373c85dc5d7d57ccd3e016e33be99f&expires=2022-02-23+10%3A32%3A33&token=NHDIONIEAOCBCNEKDFEALKMOEIIHJELICGIAOGGCKLIKFJMLOFIPADHBGIFNCHEEOGGHLAIMEAINGGOPMMKPIKGHFPFEILNLKKEMCEPAPOOHAPIINCPLPIOMCNAIKNBFBPLIPOKDJODMPPEDIJIPACNKBDDBIFJMDPAFEMEMNEPIFBAEDEOJMFMHOOMIJAJLGJJDKILICDALADDKLHCJBBPKGGKPOKMFDCGJPOJBDJBEHABEDHADJDMPFKKPAGJG&storepath=/&version=2";
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
System.out.println("------------开始下载-------------");
ProtoEntityUtil.DownloadResponse downloadResponse = ProtoEntityUtil.DownloadResponse.parseFrom(response.getEntity().getContent());
List<ProtoEntityUtil.EntityFile> fileList = downloadResponse.getFileList();
System.out.println("code: " + downloadResponse.getCode());
System.out.println("version: " + downloadResponse.getVersion());
System.out.println("FileList.size: " + fileList.size());
int i = 0;
for (ProtoEntityUtil.EntityFile file : fileList) {
System.out.println("==============第" + ++i + "遍历============");
System.out.println("*****开始解析*****");
ProtoEntityUtil.EntityFile entityFile = ProtoEntityUtil.EntityFile.parseFrom(file.toByteArray());
try{
ProtoEntityUtil.EntityFileId entityFileId = ProtoEntityUtil.EntityFileId.parseFrom(entityFile.getId());
System.out.println("id1: " + entityFileId.getSn());
if ( entityFileId.getSn() == 0) {
ProtoEntityUtil.EntityFileValue_YuanShuJu yuanshuju = ProtoEntityUtil.EntityFileValue_YuanShuJu.parseFrom(entityFile.getValue());
System.out.println("原数据: " + yuanshuju.getSortstr());
} else {
ProtoEntityUtil.EntityFileValue entityFileValue = ProtoEntityUtil.EntityFileValue.parseFrom(entityFile.getValue());
System.out.println("value1: " + entityFileValue.getContext());
}
}catch (Exception e){
System.out.println("id:"+file.getId().toStringUtf8());
}
}
System.out.println("-------下载结束------------------");
}
}