How to call a third-party API to upload pictures.

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[how to call a third-party API to upload pictures.]

Hello, I'm he Shuang, a student of the 11th phase of Zhengzhou branch of it Academy. I'm an honest, pure and kind back-end programmer. Today, I'd like to share with you the knowledge points in deep thinking - how to call a third-party API to upload pictures.

1. Background introduction

In the development process, whether it is a web page or an app, there will basically be business needs for image upload. The most typical is that users need to upload avatars after registering an account or upload ID photos when users verify.

There are two ways to upload pictures in Java:

One is to use JSP + controller to upload pictures.

One is to upload pictures using jsp + Servlet;

There are basically two forms of storage

The blob type pictures are directly stored in the database, or the pictures are stored on the server and the path corresponding to the storage box is stored in the database.

Now the mainstream project development uses the latter method. At this time, we need to use the third-party API

So what is an API?

API is a set of subroutine definitions, protocols and tools used to build application software. Generally speaking, this is a set of well-defined communication methods between various software components.

2. Knowledge analysis

Third parties usually have APIs and SDKs for us to use. So what is SDK?

SDK (software development kit) in Chinese is "software development tool group", which is used to help a product and platform develop application programs,

Provided to developers by the manufacturer of the product. The collection of relevant documents, examples and tools to assist in the development of a certain type of software can be called "SDK".

Alibaba cloud object storage OSS used in the task. Tell me about OSS.

Alibaba cloud object storage service (OSS) is a massive, secure, low-cost and highly reliable cloud storage service provided by Alibaba cloud.

翻译错误 TIMEOUT

Simple management. OSS is suitable for storing any type of files and is suitable for various websites, development enterprises and developers.

In spring MVC, the basic idea of uploading pictures using jsp + controller is as follows:

(1) Use multipartfile class to receive uploaded pictures;

(2) Store the picture on the server (alicloud OSS) and return a picture path;

(3) The picture path is stored in the user information for calling.

3. Frequently asked questions

(1) The form submits files of multipart file type, while the third-party API requires files of file type. How to convert them?

(2) How to upload pictures by calling alicloud API

4. Solutions

(1) The first method:

multipartfile file = xxx;

Commonsmultipartfile cf= (Commonsmultipartfile)file;

DiskFileItem fi = (DiskFileItem)cf.getFileItem();

File f = fi. getStoreLocation();

A file is generated in the temporary folder at the root of the project.

The second method:

transferTo(File dest);

A new file will be generated in the project;

The third method:

File f = (file) XXX strong rotation is enough. The precondition is to configure multipartresolver, otherwise the exception of type conversion failure will be reported.

The fourth method:

Workbook wb = Workbook. getWorkbook(xxx .getInputStream());

Convert to input stream and read directly;

The fifth method:

byte[] buffer = myfile. getBytes();

Convert to byte array first.

(2) See the coding practice of the fourth module for details

5. Coding practice

6. Expand thinking

Summary of skills for integrating third-party interfaces:

In fact, the calling interface is almost the same:

Send request (send request to the third party and pass parameters) - receive data (receive the returned data from the third party) - data processing (data analysis) - return to the front end - front end processing (the front end shows the encapsulated data).

1) Reading API documents: it mainly depends on the request parameters and return data. Some documents have many demos, which will help us a lot.

Refer to this: http://www.pmcaff.com/discuss/index/480006465919040?pmc_param=1

2) JSON data parsing: net fs. JSON or Ali's fastjson or Jackson degnd

Specific reference: https://www.cnblogs.com/huoer33/p/6638378.html

3) Call the tools used by the third party: httpclient, urlconnection or resttemplate

For details, refer to:

7. References

https://blog.csdn.net/qq_41802128/article/details/82918811

https://blog.csdn.net/qq_41810013/article/details/80729700

8. More discussion

Q1: questioner: Zhang Yaqiang:

What are the similarities and differences between Tencent COS and Alibaba OSS third-party object storage?

A1: respondent (he Shuang):

Personally, there is no big difference between the two. Personally, both are just a remote memory. OSS can add watermarks, thumbnails, anti-theft chains, etc. but cos can't add them. It needs to introduce a third-party jar package.

Q2: questioner: Zhang Yaqiang

Can I upload pictures through character stream?

A2: respondent (he Shuang):

No, because the picture is binary coded.

Q3: questioner: Zhou Honghao

How does the interceptor intercept pictures?

A3: respondent (he Shuang):

Just add enctype = "multipart / form data" to from.

9. Acknowledgment

Thanks to Zhang Yaqiang and Jin Liangjin. This tutorial is based on their previous technology sharing.

10. Conclusion

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>