The first file upload can only use form submission, which requires a few jar package Commons-io-1.3.2.jar,commons-fileupload-1.2.1jar, the code of the page is as follows:
<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title><Scripttype= "Text/javascript"></Script></Head><Body></Head><Body></Div><formAction= "pp"enctype= "Multipart/form-data"Method= "POST"><inputtype= "File"name= "image"><inputtype= "Submit"value= "Submit"></form></Body></HTML>
The developed framework uses the SPRINGMVC framework
The code for the backend is as follows:
@RequestMapping (value= "pp") PublicString Reg5 (@RequestParam ("image") commonsmultipartfile[] files,httpservletrequest request) {String path=Request.getcontextpath (); String Str=NULL; for(inti = 0;i<files.length;i++){ if(!Files[i].isempty ()) {STR= Request.getsession (). Getservletcontext (). Getrealpath ("/") + "fileupload\\temp" +NewDate (). GetTime () +Files[i].getoriginalfilename (); System.out.println (str); intPre = (int) System.currenttimemillis (); String filename=Files[i].getoriginalfilename (); Try {
This tool Class I do not provide, is to write files to local files. Fileutils.writebytearraytofile (NewFile (Str,filename), Files[i].getbytes (),true); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } return"Success";}
The files passed in the SPRINGMVC framework are large and may not pass through, and you can add a configuration to the spring configuration file. As follows:
Required commonsmultipartfile parameter ' file ' is not present
When you report this error, you can add a configuration to the project because the number of bytes is too large
<BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Propertyname= "Maxuploadsize"value= "104857600" /> < Propertyname= "Maxinmemorysize"value= "4096" /> </Bean>
In fact, if the file is a picture, if you want to exist in the database, you can generate a base64 picture, just a string .... can be stored directly.
Take it out and put it in SRC in the image tag to show the image, but the pixel may have an effect, and you can set it up when you build it.
How to generate Base64 pictures, there will be a separate description in my blog.
Uploading of files in Java (simple operation)