上传和下载的测试代码
作者:互联网
上传和下载的测试代码
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 {
/**
* 上传方法
* 参数为 表单形式 多为 post请求
*/
@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");
//builder.addBinaryBody("Id222",inputStreamBody.getInputStream());
System.out.println("builder : "+builder.toString());
System.out.println("---------开始上传----------");
// 创建HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// 创建URL
String url =
//"http://cs.10jqka.com.cn/multiStorage?reqtype=upload&appname=blockstock&storepath=/hq";
"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 httpPost = new HttpPost(url);
httpPost.setEntity(builder.build()); // 将表单封装进请求
// 执行请求
response = httpclient.execute(httpPost);
// 返回报文 通过uploadresponse
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("-------------上传结束-----------------");
}
/**
* 下载方法
* 请求下载基本少有参数, 多为get请求
*/
@Test
public void downLoadTest() throws IOException {
// 创建HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// 创建URl
String url =
//"http://cs.10jqka.com.cn/multiStorage?reqtype=download&appname=pc_head&userid=622241656&sessionid=1cf373c85dc5d7d57ccd3e016e33be99f&expires=2022-02-23+10%3A32%3A33&token=NHDIONIEAOCBCNEKDFEALKMOEIIHJELICGIAOGGCKLIKFJMLOFIPADHBGIFNCHEEOGGHLAIMEAINGGOPMMKPIKGHFPFEILNLKKEMCEPAPOOHAPIINCPLPIOMCNAIKNBFBPLIPOKDJODMPPEDIJIPACNKBDDBIFJMDPAFEMEMNEPIFBAEDEOJMFMHOOMIJAJLGJJDKILICDALADDKLHCJBBPKGGKPOKMFDCGJPOJBDJBEHABEDHADJDMPFKKPAGJG&storepath=/hq";
"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 httpGet = new HttpGet(url);
// 执行请求
response = httpclient.execute(httpGet);
System.out.println("------------开始下载-------------");
// 返回报文 DownloadResponse
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{
//System.out.println("entityFile.getId(): " + entityFile.getId());//<ByteString@25d250c6 size=2>
ProtoEntityUtil.EntityFileId entityFileId = ProtoEntityUtil.EntityFileId.parseFrom(entityFile.getId());
System.out.println("id1: " + entityFileId.getSn());
//ProtoEntityUtil.EntityFileValue_YuanShuJu yuanshuju = ProtoEntityUtil.EntityFileValue_YuanShuJu.parseFrom(entityFile.getValue());
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());
//System.out.println("value2: " + new String(entityFileValue.getContext().getBytes()));
}
}catch (Exception e){
System.out.println("id:"+file.getId().toStringUtf8());
}
//System.out.println("file.getId(): " + new String(file.getId().toByteArray()));
//System.out.println("file.getvalue(): " + new String(file.getValue().toByteArray()));
//ProtoEntityUtil.EntityFile entityFile = ProtoEntityUtil.EntityFile.parseFrom(file.toByteArray());
//ByteString id = entityFile.getId();
//ByteString value = entityFile.getValue();*/
/* try{
ProtoEntityUtil.EntityFileId entityFileId = ProtoEntityUtil.EntityFileId.parseFrom(file.getId());
System.out.println("entityid :" + entityFileId.getSn());
}catch (Exception e){
System.out.println("id:"+file.getId().toStringUtf8());
}*/
/* ProtoEntityUtil.EntityFileValue_YuanShuJu yuanshuju = ProtoEntityUtil.EntityFileValue_YuanShuJu.parseFrom(file.getValue());
System.out.println("原数据:" + yuanshuju.getSortstr());*/
/* System.out.println("id:" + id.toString());
System.out.println("value: "+ value.toString());*/
}
System.out.println("-------下载结束------------------");
}
}
标签:ProtoEntityUtil,System,println,file,测试代码,import,上传,下载,out 来源: https://blog.csdn.net/weixin_45866849/article/details/123081721