Upload files by sending multipart request programmatically
2017-02-08 07:45
567 查看
In the article
Upload file to servlet without using HTML form,
we discussed how to fire an HTTP POST request to transfer a file to a server – but that request’s content type is not of
multipart/form-data, so it may not work with the servers which handle multipart request and it requires both client and server are implemented in Java. To overcome that limitation, in this article, we are going to discuss a different solution for uploading
files from a Java client to any web server in a programmatic way, without using upload form in HTML code. Let’s examine this interesting solution now.
MultipartUtility with the following code:
This utility class uses
java.net.HttpURLConnection class and follows the
RFC 1867 (Form-based File Upload in HTML) to make an HTTP POST request with
multipart/form-data content type in order to upload files to a given URL. It has one constructor and three methods:
MultipartUtility(String requestURL, String charset): creates a new instance of this class for a given request URL and charset.
void
addFormField(String name, String value): adds a regular text field to the request.
void
addHeaderField(String name, String value): adds an HTTP header field to the request.
void
addFilePart(String fieldName, File uploadFile): attach a file to be uploaded to the request.
List<String>
finish(): this method must be invoked lastly to complete the request and receive response from server as a list of String.
Now let’s take a look at an example of how to use this utility class.
MultipartUtility class abstracts all the detailed implementation, a usage example would be pretty simple as shown in the following program:
In this program, we connect to the servlet’s URL of the application FileUploadSpringMVC (see this tutorial:
Upload files with Spring MVC):
http://localhost:8080/FileUploadSpringMVC/uploadFile.do We added two header fields, two form fields and two upload files under the name “fileUpload” – which must match the fields declared in the
upload form of the FileUploadSpringMVC application. When running the above program, it will produce the following output:
We can realize that the server’s response is actually
HTML code of the application FileUploadSpringMVC’s
result page. So far in this article, we’ve discussed about how to implement a command line program in Java which is capable of upload files to any URL that can handle multipart request, without implementing an HTML upload form. This would be very useful
in case we want to upload files to a web server programmatically.
Upload file to servlet without using HTML form,
we discussed how to fire an HTTP POST request to transfer a file to a server – but that request’s content type is not of
multipart/form-data, so it may not work with the servers which handle multipart request and it requires both client and server are implemented in Java. To overcome that limitation, in this article, we are going to discuss a different solution for uploading
files from a Java client to any web server in a programmatic way, without using upload form in HTML code. Let’s examine this interesting solution now.
Coding multipart utility class
We build the utility class calledMultipartUtility with the following code:
java.net.HttpURLConnection class and follows the
RFC 1867 (Form-based File Upload in HTML) to make an HTTP POST request with
multipart/form-data content type in order to upload files to a given URL. It has one constructor and three methods:
MultipartUtility(String requestURL, String charset): creates a new instance of this class for a given request URL and charset.
void
addFormField(String name, String value): adds a regular text field to the request.
void
addHeaderField(String name, String value): adds an HTTP header field to the request.
void
addFilePart(String fieldName, File uploadFile): attach a file to be uploaded to the request.
List<String>
finish(): this method must be invoked lastly to complete the request and receive response from server as a list of String.
Now let’s take a look at an example of how to use this utility class.
Coding a test program
Since theMultipartUtility class abstracts all the detailed implementation, a usage example would be pretty simple as shown in the following program:
Upload files with Spring MVC):
http://localhost:8080/FileUploadSpringMVC/uploadFile.do We added two header fields, two form fields and two upload files under the name “fileUpload” – which must match the fields declared in the
upload form of the FileUploadSpringMVC application. When running the above program, it will produce the following output:
We can realize that the server’s response is actually
HTML code of the application FileUploadSpringMVC’s
result page. So far in this article, we’ve discussed about how to implement a command line program in Java which is capable of upload files to any URL that can handle multipart request, without implementing an HTML upload form. This would be very useful
in case we want to upload files to a web server programmatically.
相关文章推荐
- Upload files by sending multipart request programmatically
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
- Upload multiple files Hotmail style By Kwame Hubbard
- MultipartHttpServletRequest,ajaxFileUpload上传文件,读取文件乱码问题
- Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
- 遇到问题-----uploadify上传文件ServletFileUpload.isMultipartContent(request)为false
- jsp upload package "MultipartRequest and MultipartParser"的license是一本书,呵呵,有趣
- servlet: org.apache.tomcat.util.http.fileupload.FileUploadException: Processing of multipart/form-data request failed
- 异常-----springmvc + ajaxfileupload解决ajax不能异步上传图片的问题。java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
- org.apache.commons.fileupload.FileUploadException: Processing of multipart/form-data request failed解决
- Unable to parse request org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. null
- Caused by java.lang.NoClassDefFoundError orgapachecommonsfileuploadRequestContext
- 解决 Processing of multipart/form-data request failed. /upload/A.tmp (No such file or directory) 问题
- Required MultipartFile parameter 'uploadFiles' is not present
- How to upload a file via a HTTP multipart request in Java without using any external libraries
- Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- How to simulate a Form POST request by using WinInet
- http 400 Bad Request The request sent by the client was syntactically incorrect