File upload and download method using httpclient

1 HTTP

HTTP protocol is probably the most widely used and important protocol on the Internet. More and more Java applications need to access network resources directly through HTTP protocol.

Although in JDK Java Net package has provided the basic functions of accessing HTTP protocol, but for most applications, the functions provided by JDK library itself are not rich and flexible enough. Httpclient is used to provide an efficient, up-to-date and feature rich client programming toolkit supporting HTTP protocol, and it supports the latest version and recommendations of HTTP protocol.

Generally, we use chrome or other browsers to access a web server to browse pages, view information, submit some data, upload and download files, etc. Some of these pages visited are just ordinary pages, some can only be used after users log in, or need authentication, and some are transmitted through encryption, such as HTTPS. At present, the browsers we use to deal with these situations will not pose a problem. But what if we need to access the server's resources without a browser? What should I do?

Let's take the upload and download of files initiated by the local client as an example to make a small demo. Httpclient has two forms, one is org apache. HTTP, one is org apache. commons. httpclient. HttpClient。

2 file upload

File upload can be realized in two ways: postmethod and httppost. The treatment of the two is similar. Postmethod uses filebody to wrap the file stream, and httppost uses filepart to wrap the file stream. When passing the file stream to the server, you can pass other parameters at the same time.

2.1 client processing

2.1. 1. Postmethod method

Encapsulate the file into filepart and put it into the part array. At the same time, other parameters can be put into stringpart. It is not written here, but simply set the parameters in the way of setparameter. Httpclient here is org apache. commons. httpclient. HttpClient。

Remember to release the connection through releaseconnection after doing it.

2.1. 2 httppost mode

This method is similar to the above, but becomes filebody. The above part array corresponds to httpentity here. Httpclient here is org apache. http. client. Methods.

2.2 server processing

No matter which upload method the client uses, the processing of the server is the same. After obtaining the parameters through HttpServletRequest, the obtained items are classified into ordinary forms and file forms.

The size and encoding format of uploaded files can be set through servletfileupload.

In short, the server processes the parameters as HTML forms.

After processing, the server can set the simple information returned to the client in the header. If the returned client is a stream, the size of the stream must be set in advance!

response. setContentLength((int) file. length());

3 file download

The file can be downloaded using the getmethod of httpclient, httpget and the original httpurlconnection.

3.1 client processing

3.1. 1. Getmethod method

Httpclient here is org apache. commons. httpclient. HttpClient。

3.1. 2 httpget mode

Httpclient here is org apache. http. client. Methods.

3.1. 3 httpurlconnection mode

3.2 server processing

Although the processing methods of the client are different, the server is the same.

4 Summary

The most basic function of httpclient is to execute the HTTP method. The execution of an HTTP method involves the interaction of one or more HTTP requests / HTTP responses. Usually, this process will be automatically processed by httpclient and transparent to users. The user only needs to provide the HTTP request object, and the httpclient will send the HTTP request to the target server and receive the response from the server. If the HTTP request is not executed successfully, the httpclient will throw an exception. So pay attention to finally when writing code.

All HTTP requests have a request line, including method name, request URI and HTTP version number. Httpclient supports all HTTP methods defined in http / 1.1: get, head, post, put, delete, trace and options. Post is used for upload above, and get is used for download.

For now, use org apache. commons. httpclient. More httpclient. Look at yourself~

The above is all the contents of the file upload and download method using httpclient brought by Xiaobian. I hope you can support more programming tips~

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
分享
二维码
< <上一篇
下一篇>>