Today I met a hole, here to introduce you. Many file upload types are now multipart/form-data types, and HTTP requests are as follows:
The problem, however, is that if you use a traditional Struts2 or servlet, you can easily implement the function of file reception, such as the following code:
Boolean ismultipart = servletfileupload.ismultipartcontent (request);//Determine if the form file type
diskfileitemfactory factory = New Diskfileitemfactory ();
Servletfileupload SFU = new Servletfileupload (factory);
List items = sfu.parserequest (request),//request to get all uploaded domain lists for
(Iterator iter = Items.iterator (); Iter.hasnext () ;) {
Fileitem Fileitem = (fileitem) iter.next ();
if (!fileitem.isformfield () &&fileitem!=null) {//interpretation is not a normal form field, which is a file
System.out.println ("Name:" + Fileitem.getname ());
}
But today I put this piece of code on the springboot above the time and how to receive the file information, always thought is the front-end what data transmission is wrong. Later only to know that the original springboot has its own code to receive the request. Here is a detailed description of how it is to achieve this function. Preferred to do a simple case, that is, a single file upload case. (this case is based on the springboot above, so we first have to build a good springboot this framework) front-Office HTML code:
Background Receive code:
/** * File upload specific implementation method; * * @param file * @return */@RequestMapping ("/upload") @ResponseBody Pub
Lic String handlefileupload (@RequestParam ("file") Multipartfile file) {if (!file.isempty ()) { try {/* * After this code is executed, the image is uploaded to the project's follow-up path; you spread your mind, if we want to upload the picture to * d :/files can we achieve this?
And so on * Here is just one example, please self-reference, into the actual may require you to do some thinking, such as: 1, File path, 2, filename; * 3, file format;
4, the file size limit; */Bufferedoutputstream out = new Bufferedoutputstream (New Fileoutputstre
AM (New File (File.getoriginalfilename ())));
System.out.println (File.getname ());
Out.write (File.getbytes ());
Out.flush ();
Out.close ();
} catch (FileNotFoundException e) { E.printstacktrace ();
Return "Upload failed," + e.getmessage ();
} catch (IOException e) {e.printstacktrace ();
Return "Upload failed," + e.getmessage ();
} return "Upload succeeded";
} else {return ' upload failed because the file is empty. '; }
}
This enables the receipt of the Multipart/form-data type file. If there are multiple files plus multiple fields, then look at the next multiple file upload case. Front Office HTML Interface:
<! DOCTYPE html>
Background Receive code:
@RequestMapping (value = "/batch/upload", method = Requestmethod.post) @ResponseBody public String Hand Lefileupload (HttpServletRequest request) {Multiparthttpservletrequest params= ((multiparthttpservletrequest) R
Equest);
list<multipartfile> files = ((multiparthttpservletrequest) request). GetFiles ("file");
String name=params.getparameter ("name");
System.out.println ("Name:" +name);
String id=params.getparameter ("id");
SYSTEM.OUT.PRINTLN ("ID:" +id);
Multipartfile file = null;
Bufferedoutputstream stream = null;
for (int i = 0; i < files.size (); ++i) {file = Files.get (i);
if (!file.isempty ()) {try {byte[] bytes = File.getbytes ();
stream = new Bufferedoutputstream (New FileOutputStream (New File (File.getoriginalfilename ()));
Stream.Write (bytes);
Stream.Close ();
} catch (Exception e) {stream = null;
Return "You failed to upload" + i + "= +" + e.getmessage (); }} else {return ' failed to upload ' + i + ' be
Cause the file was empty. ";}}
Return "Upload successful"; }
This enables the ability to receive multiple files. Springboot can also be the format of receiving files and so on to limit, I do not say much here, we are interested to understand their own. Be sure to remember springboot to multipart/form-data type of File receive and other is not the same, we will encounter the time to be careful, do not like me as the indomitable of stepping in also silly thought is the front-end error.
If you have any questions or doubts about the article, you can add my subscription number on the above message, subscription number above I will regularly update the latest blog. If it's too much trouble, just add me to Wechat:lzqcode .