In the recent development of C/s, you need to upload files to the Java background for processing.
For larger files we can directly use the FTP protocol file, smaller files can be the same as B/s with HTTP upload.
First of all, we need to use POST to send the data because we want to pass the file. Get has a length limit, and the data follows the URL.
Now that we're sending a POST request, we'll look at the message format of the POST request.
Introduction to HTTP messages
First write a simple HTML page to send a form to observe the post message it sends, the form contains an uploaded file and the text of the file description.
<! DOCTYPE html>
The message format on the Chrom is as follows:
Post/form http/1.1 Host:www.baidu.com connection:keep-alive content-length:2417 Cache-control: Max-age=0 accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Origin: Null upgrade-insecure-requests:1 user-agent:mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 content-type:multipart/ Form-data; boundary=----WEBKITFORMBOUNDARYM4LGQCTCCIBILNPT accept-encoding:gzip, deflate ACCEPT-LANGUAGE:ZH-CN, zh;q=0.8 cookie:baiduid=9a110f7f907aeac501cd156dde0ea380:fg=1 ------ WEBKITFORMBOUNDARYM4LGQCTCCIBILNPT content-disposition:form-data; name= "file"; filename= "Close.png" Content-type:image/png contains the binary data of the picture ------WEBKITFORMBOUNDARYM4LGQCTCCIBILNPT Content-disposition:form-data; Name= "description" This is a image ------webkitformboundarym4lgqctccibilnpt--
The HTTP message consists of three parts : The start line, which describes the message, contains the header block of the attribute, and optionally, the body (body) part that contains the data.
the starting line format for the request message is <method> <request-URL> <version>
Post/form http/1.1
Method: For the client want the server to the resources of the action, usually get, POST, head and so on.
Request URL: The absolute path to the resource, which is determined by the form action.
Version: The HTTP version used to protect the message, such as 1.1, 1.0.
HTTP Header Block
There can be 0 or more headers, each header containing a name, followed by a colon (:), followed by an optional space, followed by a value, and finally a CRLF (/r/n). The header is terminated by a blank line (CRLF). Represents the end of the header list and the beginning of the entity body part. When you construct the message, be sure to pay attention to the addition of lines and blank lines, so as not to cause formatting errors. In HTTP 1.1, a specific header must be included in a valid request or response. The request header is as follows:
Host:www.baidu.com connection:keep-alive content-length:2417 cache-control:max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 user-agent:mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 content-type:multipart/ Form-data; boundary=----WEBKITFORMBOUNDARYM4LGQCTCCIBILNPT accept-encoding:gzip, deflate Accept-language:zh-cn,zh; q=0.8
Briefly explain a few of the headers:
Host: The server address that receives the request
Connection: Allows clients and servers to specify options related to a request/response connection, keep-alive represents a persistent connection
Content-length: The size of the entity body, which must be set when constructing the message
CacheControl: Controlling the behavior of the cache
Accept: Media types that the user agent can handle
User-agent:http client program information, browser information
Content-type: The media type of the entity principal, and the file upload in the form should be set to Multipart/form-data. Boundary is important, which is a boundary that identifies the file stream and is used to identify where the file starts and ends.
Accept-encoding: is the browser sent to the server, declaring the browser supported encoding type
Accept-language declaring browser-supported languages
HTTP Data Body
This section is what HTTP is going to transfer.
The starting boundary is the value set in the Content-type, boundary is used as the boundary between the request parameters, there is a clear boundary between multiple parameters, so that the server can correctly resolve to the parameters. It has a format requirement, and it must start with----the different browsers produce different boundary, but the front has to be--。
Content-disposition is to provide a default file name when the user wants to save the requested content as a file.
The middle is the data we transmit.
Finally add a boundary--, don't forget the last--。
So the message is structured and finished.
Send a POST request in C # private void Uploadrequest (string url, string filePath) {//timestamp, used as boundary string Timestam p = DateTime.Now.Ticks.ToString ("x"); Creates a HttpWebRequest object based on a Uri HttpWebRequest httpreq = (HttpWebRequest) webrequest.create (new Uri (URL)); Httpreq.method = "POST"; Httpreq.allowwritestreambuffering = false; Do not use cache for sent data httpreq.timeout = 300000; Set time-out for response (300 seconds) Httpreq.contenttype = "multipart/form-data; boundary= "+ timeStamp; File FileStream FileStream = new FileStream (FilePath, FileMode.Open, FileAccess.Read); BinaryReader BinaryReader = new BinaryReader (fileStream); Header information String boundary = "--" + timeStamp; String dataformat = boundary + "\r\ncontent-disposition:form-data; Name=\ "{0}\"; filename=\ "{1}\" \r\ncontent-type:application/octet-stream\r\n\r\n "; String Header = String. Format (DataFormat, "file", Path.getfilenaMe (FilePath)); byte[] postheaderbytes = Encoding.UTF8.GetBytes (header); End Border byte[] boundarybytes = Encoding.ASCII.GetBytes ("\r\n--" + TimeStamp + "--\r\n"); Long length = filestream.length + Postheaderbytes.length + boundarybytes.length; Httpreq.contentlength = length;//Request Content Length try {//upload 4k int bufferlengt each time h = 4096; byte[] buffer = new Byte[bufferlength]; Number of bytes uploaded long offset = 0; int size = binaryreader.read (buffer, 0, bufferlength); Stream Poststream = Httpreq.getrequeststream (); Send Request Header Message Poststream.write (postheaderbytes, 0, postheaderbytes.length); while (Size > 0) {poststream.write (buffer, 0, size); Offset + = size; Size = binaryreader.read (buffer, 0, bufferlength); }//Add trailing boundary poststream.write (boundarybytes, 0, boundarybytes.length); Poststream.close (); Gets the server-side response using (httpwebresponse response = (HttpWebResponse) httpreq.getresponse ()) { Stream Receivestream = Response. GetResponseStream (); StreamReader Readstream = new StreamReader (Receivestream, Encoding.UTF8); String returnvalue = Readstream.readtoend (); MessageBox.Show (returnvalue); Response. Close (); Readstream.close (); }} catch (Exception ex) {Debug.WriteLine ("File Transfer exception:" + ex. Message); } finally {filestream.close (); Binaryreader.close (); } }Java side Receive RequestPublic Map savecapture (httpservletrequest request, httpservletresponse response, Map config) throws Exception {Res Ponse.setcontenttype ("Text/html;charset=utf-8"); Read request body byte[] BODY = readbody (request); Gets the string representation of all body contents As String textbody = new String (Body, "iso-8859-1"); Gets the uploaded file name String fileName = GetFileName (TextBody); Get file start and end position String ContentType = Request.getcontenttype (); String Boundarytext = contenttype.substring (contenttype.lastindexof ("=") + 1, contenttype.length ()); Get the momentum and end position of the actual upload file int pos = Textbody.indexof ("filename=\"); pos = Textbody.indexof ("\ n", POS) + 1; pos = Textbody.indexof ("\ n", POS) + 1; pos = Textbody.indexof ("\ n", POS) + 1; int boundaryloc = Textbody.indexof (Boundarytext, POS)-4; int begin = ((textbody.substring (0, POS)). GetBytes ("Iso-8859-1")). Length; int end = ((textbody.substring (0, Boundaryloc)). GetBytes ("ISO-8859-1 ")). Length; Save to local Writetodir (filename,body,begin,end); Response.getwriter (). println ("success!"); return config; } private byte[] Readbody (HttpServletRequest request) throws IOException {//Get request text byte length int formdatalengt h = request.getcontentlength (); Gets the ServletInputStream input stream object DataInputStream DataStream = new DataInputStream (Request.getinputstream ()); byte body[] = new Byte[formdatalength]; int totalbytes = 0; while (TotalBytes < formdatalength) {int bytes = Datastream.read (Body, totalbytes, formdatalength); TotalBytes + = bytes; } return body; } private String GetFileName (String requestbody) {string fileName = requestbody.substring (requestbody.index Of ("filename=\") + 10); FileName = filename.substring (0, Filename.indexof ("\ n")); FileName = filename.substring (Filename.indexof ("\ n") + 1, filename.indexof ("\" ")); Return filename; private void Writetodir (String fileName, byte[] body, int begin, int end) throws IOException {fileoutputs Tream FileOutputStream = new FileOutputStream ("d:/" + fileName); Fileoutputstream.write (body, Begin, (End-begin)); Fileoutputstream.flush (); Fileoutputstream.close (); }
When using Request.getparameter (), pay attention to the MIME type of the data passed in.
When the GET method is submitted, the form items are saved in the header, in the form of a string such as http://localhost:8080/form?key1=value1&key2=value2. The server side can fetch values by Request.getparameter ("Key1").
POST mode, if Enctype application/x-www-form-urlencoded, the form data is stored in the HTTP data body, formatted like this: with Request.getparameter () can be taken to the data.
But if Enctype is Multipart/form-data, as in the above, the form data is stored in the HTTP data body, separated by boundary between the individual data items. With Request.getparameter () is not the data, then need to request.getinputstream to manipulate the flow of data, you need to parse the stream to get the table items and upload the contents of the file and other information.
This requirement is a more common feature, so there are many open source components that can be used directly. For example: Apache FileUpload components, smartupload and so on. With the APIs provided by these open source upload components, you can get the specified form items directly from the request.
When returning a value, you can only return a byte stream or a character stream, and you cannot get Response.getwriter (), Response.getoutputstream () at the same time.
Reference:
"HTTP authoritative Guide"
Http://www.cnblogs.com/txw1958/archive/2013/01/11/csharp-HttpWebRequest-HttpWebResponse.html
Http://www.cnblogs.com/loujady/archive/2011/09/13/2174695.html
Http://my.oschina.net/Barudisshu/blog/150026?fromerr=aaqkzmRKUploading files to a Java server with HTTP in C # clients