Request in Java-form form with file upload

Source: Internet
Author: User
Tags uuid

Common system development always avoid showing pictures, save some files and other operations.

Behind these operations is the enctype= "Multipart/form-data" type of form that programmers are most familiar with.

Speaking of file type input, we often encounter problems:

The most common is the encoding format-Chinese garbled. The best way to solve Chinese garbled characters is to use the International Code of the UTF-8 format for all of our projects because it is a variable byte length encoding, which is more than enough for 2 bytes of Chinese. The other is to use string s2 = new String (S1.getbytes ("iso-8859-1"), "GBK"); Convert the server encoding (in Tomcat, for example) to GBK Chinese encoding.

The second is the question of request receiving parameters. First look at the segment code

//-----Code Snippets SPINGMVC uploading FilesMultiparthttpservletrequest mrequest = (multiparthttpservletrequest) request;//Request Cast Notemultipartfile File= Mrequest.getfile ("File"); if(!File.isempty ()) {String FileName=File.getoriginalfilename (); if(Stringutils.isnotblank (FileName)) {//because the last added control did not upload the appropriate contentString uuid =Uuid.randomuuid (). toString (); String FileType=filename.substring (fileName lastIndexOf (".")); //use characters to replace picture names to prevent garbledString tempname = uuid.substring (1, 10) +FileType; Uploadfilepath=fileroothelper.getorigimage ()+Tempname; File UploadFile=NewFile (BasePath+ Uploadfilepath);//Upload AddressFile Mir=NewFile (BasePath+ fileroothelper.getorigimage ());//file path                            if(!mir.exists ())                            {Mir.mkdirs (); } file.transferto (UploadFile);//Start uploading}

Executes multiparthttpservletrequest mrequest = (multiparthttpservletrequest) request, when the line code does not contain the file type of input , it will inevitably produce anomalies.

The request requests are SPRING-MVC configuration files, and the exception has been generated before the request method is entered. So, the problem comes??

How to avoid abnormal production?

A more intuitive approach is to:

<formAction= "Iconupload.action"Method= "POST"enctype= "Multipart/form-data">  <P>First Name:<inputtype= "text"name= "FName" /></P>  <P>Last Name:<inputtype= "text"name= "LName" /></P>  <P>Icon<inputtype= "File"name= "Ficon"/></P>          <inputtype= "Submit"value= "Submit" /></form>

<input type= "file" name = "Ficon"/> not selected by background file.isempty () , rather than in front of the judgment.

There is also a way to do the front-end judgment or not, the back end again to determine whether there are files flow, respectively, write 2 kinds of processing methods. The specific code is not posted, and the individual did not find the way to implement code-_-!

Personal sentiment:

Each person's thinking mode is not the same, the problem-solving mentality will be different.

But after all, it revolves around ' problem solving ' and is good at learning other people's thinking patterns and making up for their disdain. This will solve the problem quickly and efficiently. As the saying goes: Not to do, only unexpected.

To accept others ' opinions and understand others is not only helpful to their growth, but also the embodiment of personal accomplishment and value. Multi-thinking is an indispensable treasure in everyone's life!!

Request in Java-form form with file upload

Related Article

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.