preface
I don’t know if you have encountered uploading and downloading at the code level in your work. The general scenario is to call the third-party interface to upload and download large files.
A little buddy of mine recently encountered in his work. He needed to call the third party HTTP interface in code to upload the original file, and then called the third party interface to download the data files processed by the third party service to the local. In fact, there is no technical difficulty, he said. Baidu has a lot of code examples. Httpclient supports uploading and downloading files, but the code is too much and not very elegant.
He showed me the upload code of httpclient:
String uploadUrl = "http://xxxxx.com/upload";
HttpPost httpPost = new HttpPost(uploadUrl);
FileBody fileBody = new FileBody(new File("C:/Users/Administrator/Desktop/source.excel"));
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.addPart("file",fileBody);
//Set other parameters
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new NameValuePair("Accept","application/json, text/plain, */*"));
nvps.add(new NameValuePair("Accept-Encoding","gzip, deflate, br"));
nvps.add(new NameValuePair("Accept-Language","zh-CN,zh;q=0.9"));
nvps.add(new NameValuePair("Connection","keep-alive"));
nvps.add(new NameValuePair("Content-Length","28700"));
nvps.add(new NameValuePair("Content-Type","multipart/form-data; boundary=----WebKitFormBoundarypaEfQmIQBbUrkI0c"));
nvps.add(new NameValuePair("Host","xxxxx.com"));
nvps.add(new NameValuePair("Origin","http://xxxxx.com"));
nvps.add(new NameValuePair("Referer","xxxxx.com/admin/goods_edit.html"));
nvps.add(new NameValuePair("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"));
HttpEntity reqEntity = multipartEntityBuilder.build();
httpPost.setEntity(reqEntity);
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println (status code returned after upload:+ response.getStatusLine ().getStatusCode());
try {
HttpEntity resEntity = response.getEntity();
respStr = getRespString(resEntity);
EntityUtils.consume(reqEntity);
} catch (Exception e) {
e.printStackTrace();
} finally {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("resp=" + respStr);
Because to upload from the code, you need to build a multipartentitybuilder and set up various headers. My little friend asked me what framework can provide more elegant writing.
In fact, many frameworks have more concise APIs, but I still recommend a popular framework to himForest
This framework has been recommended by me beforeAn excellent HTTP framework that directly hit the pain point, let me super efficiently complete the docking with the third party interface
Forest
It is a tool framework mainly devoted to various scenarios of HTTP requests. Basically, a few lines of code can solve almost most of the HTTP scenarios. The API focuses on ease of use and provides many features, which is in line with the habits of domestic developers. Moreover, the author is also diligent in updating. The current stable release version is available for production environments.
Project homepage address:https://gitee.com/dt_flys/forest
Upload and download with forest
Forest
It can solve most of the problems in the HTTP scene. For upload and download, the author provides the upload and download function in the latest version, which can be implemented in the simplest way, greatly facilitating the developers.
For those who want to knowForest
For children’s shoes with other functions, you can go to the project home page or the article I wrote before. This is for reference onlyForest
New features for uploading and downloading.
Take the springboot project as an exampleForest
:
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>spring-boot-starter-forest</artifactId>
<version>1.4.6</version>
</dependency>
definitionRemoteDataHander
Interface:
public interface RemoteDataHander{
@Post(url = "http://xxxxx.com/upload")
void upload(@DataFile("file") File file, OnProgress onProgress);
@Get(url = "http://xxxxx.com/report/xxx.zip")
@DownloadFile(dir = "public interface RemoteDataHander{
@Post(url = "http://xxxxx.com/upload")
void upload(@DataFile("file") File file, OnProgress onProgress);
@Get(url = "http://xxxxx.com/report/xxx.zip")
@DownloadFile(dir = "${0}")
void downloadFile(String dir, OnProgress onProgress);
}
")
void downloadFile(String dir, OnProgress onProgress);
}
This interface will beForest
The scanning component scans and registers in the spring container when it starts, and then it can be called for upload and download operations just like using local methods.
ParameterOnProgress
Parameter is an interface that you can implement to complete the progress callback
File file = myClient.downloadFile("D:\TestDownload", progress -> {
System.out.println ("total bytes: " + progress.getTotalBytes ()); // file size
System.out.println ("current bytes: " + progress.getCurrentBytes ()); // bytes downloaded
System.out.println ("progress: " + Math.round ( progress.getRate () * 100) + "%"); // download percentage
if ( progress.isDone ()) {// download complete
System.out.println("-------- Download Completed! --------");
}
});
Upload and download can be achievedOnProgress
Yes, of course, you can not pass it.
Usage of some other parameters
In addition to the usage of the above examples,Forest
It also supports other types of file parameters and return parameters, such as file stream, byte array, multipartfile type, etc. The usage is as follows:
upload:
/**
*File type object
*/
@Post(url = "http://xxxxx.com/upload")
Map upload(@DataFile("file") File file, OnProgress onProgress);
/**
*Byte array
*When using byte arrays and InputStream objects, be sure to define the filename property
*/
@Post(url = "http://xxxxx.com/upload")
Map upload(@DataFile(value = "file", fileName = "") byte[] bytes, String filename);
/**
*InputStream object
*When using byte arrays and InputStream objects, be sure to define the filename property
*/
@Post(url = "http://xxxxx.com/upload")
Map upload(@DataFile(value = "file", fileName = "") InputStream in, String filename);
/**
*Multipartfile object in spring web MVC
*/
@PostRequest(url = "http://xxxxx.com/upload")
Map upload(@DataFile(value = "file") MultipartFile multipartFile, OnProgress onProgress);
/**
*Resource object of spring
*/
@Post(url = "http://xxxxx.com/upload")
Map upload(@DataFile(value = "file") Resource resource);
download:
/**
*The return type is byte [] to convert the downloaded file into a byte array
*/
@GetRequest(url = "http://localhost:8080/images/test-img.jpg")
byte[] downloadImageToByteArray();
/**
*The return type is InputStream, and the file content is read by stream
*/
@Request(url = "http://localhost:8080/images/test-img.jpg")
InputStream downloadImageToInputStream();
The byte array and input stream returned by download can be used for custom operation
Some feelings
From the perspective of users,Forest
Gives a very friendly API, and the declaration and configuration are very simple. It is very convenient for developers. Not only upload and download scenarios, but also other commonly used HTTP call scenarios,Forest
It is also a one-stop solution tool at http level. Interested students can go to have a look, will certainly improve your HTTP scene development efficiency.
I have also discussed with the author some of the current HTTP framework andForest
The development route of the industry. The author is modest and answers some of my questions
The author has always said that he hopes to make all kinds of HTTP scenarios to the extreme, so that developers can really use a few lines of code to gracefully implement complex HTTP scenarios. It’s not easy to do open source projects. I like this craftsmanship spirit. Hope forest can solve the pain point for more developers in the future.
Follow the author
I am Poseidon, a serious, warm and persistent boy. I insist on being an original technology sharing number and paying attention to “Yuanren tribe”. I will publish a practical original technology article every week to accompany you. I am no longer afraid.