Uploading of files in Java (simple operation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.