Java request-form with File Upload

Source: Internet
Author: User

Java request-form with File Upload
In common system development, Image Display and saving operations are inevitable. Behind these operations is the form that programmers are most familiar with: enctype = "multipart/form-data. When talking about file-type input, we often encounter problems: the most common is the encoding format-Chinese garbled characters. The best way to solve Chinese Garbled text is to use international UTF-8 encoding for all our projects, because it is a variable-byte-Length Encoding that is more than enough to occupy 2 bytes of Chinese characters. The other is to use String s2 = new String (s1.getBytes ("ISO-8859-1"), "GBK"); the server code (take tomcat as an example) to convert gbk Chinese encoding. The second is the request receiving parameter. First, read the code section.

// ----- Code snippet spingMVC uploads the file MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; // mandatory request conversion note MultipartFile file = mRequest. getFile ("file"); if (! File. isEmpty () {String fileName = file. getOriginalFilename (); if (StringUtils. isNotBlank (fileName) {// because the last added control does not upload the corresponding content String uuid = UUID. randomUUID (). toString (); String fileType = fileName. substring (fileName. lastIndexOf (". "); // Replace the image name with characters to prevent garbled String tempName = uuid. substring (1, 10) + fileType; uploadFilePath = fileRootHelper. getOrigImage () + tempName; File uploadfile = new File (basepat H + uploadFilePath); // upload address File mir = new File (basepath + fileRootHelper. getOrigImage (); // File path if (! Mir. exists () {mir. mkdirs ();} file. transferTo (uploadfile); // start upload

 

Execute MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request. When this line of code is executed, if the request does not contain file-type input, an exception is inevitable. However, all request requests go through the spring-mvc configuration file, and exceptions are generated before the request method is entered. As a result, the problem arises ?? How to avoid exceptions? An intuitive method is as follows:
<form action="iconUpload.action" method="post"enctype="multipart/form-data">  <p>First name: <input type="text" name="fname" /></p>  <p>Last name: <input type="text" name="lname" /></p>  <p>Icon <input type="file" name = "ficon"/></p>          <input type="submit" value="Submit" /></form>

 

<Input type = "file" name = "ficon"/> if this parameter is not selected, it is determined by the background file. isEmpty () instead of the frontend. Another approach is to determine whether to pass the data or not, and then determine whether there is any file stream at the backend. Write two processing methods respectively. The specific code will not be pasted, and individuals cannot find the implementation method code -_-! Personal perception: different people have different ways of thinking and different ideas for solving problems. But it is all about solving problems. It is good at learning others' thinking patterns and making up for your contempt. In this way, the problem can be solved quickly and efficiently. As the saying goes: You can't do it, just think of it. Accepting others' opinions with an open mind and understanding others not only helps your own growth, but also reflects your personal accomplishment and value. Multi-thinking is an indispensable fortune in everyone's life !! In the compilation process, all the actions are aimed at data access! The life cycle of a piece of data continues to run with the beginning and end of the program. For example, the program is a track, the train is the data, the database is the station, and the passenger is the data source. Dreaming of how to change the trivial things of a program, make it concise and practical, there are not so many complicated files, this is the real pursuit!

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.