Java Web file upload and java Web file upload

Source: Internet
Author: User
Tags nginx server

Java Web file upload and java Web file upload

Note: If the Request Entity is Too Large, modify the size of the Nginx server (Baidu refer to the solution of 413 Request Entity Too Large)
Jsp: <input type = "file" style = "height: 114px; width: 100%; position: absolute; opacity: 0;" name = "file1" id = "file1">

$ (Function () {$ ("# file1 "). change (function () {// create the FormData object var data = new FormData (); // Add data for the FormData object $. each ($ ('# file1') [0]. files, function (I, file) {data. append ('upload _ file', file) ;}); $. ajax ({url: '$ {pageContext. request. contextPath}/uploadimag', type: 'post', data: data, cache: false, contentType: false, // processData: false, // success: function (data) {// display the append var imgUrl = "/" + data; var showImg = "<div class = 'img-item'>" + " </span> "+" </div> "; $ ("# addImg "). before (showImg); // data appending var inputImg = "<input type = 'den den 'name = 'postimag' value = '" + imgUrl + "'> "; $ ("# typeId "). after (inputImg );}});});});

Java Background:

1 @ RequestMapping (value = "/uploadImage", method = RequestMethod. POST) 2 @ ResponseBody 3 public String uploadImage (HttpServletRequest request) throws IOException {4 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 5 Iterator <String> iterator = multipartRequest. getFileNames (); 6 String fileName = ""; 7 SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); 8 St Ring dir = "upload/" + sdf. format (new Date () + "/"; 9 String realPath = request. getSession (). getServletContext (). getRealPath ("/"); 10 while (iterator. hasNext () {11 MultipartFile multipartFile = multipartRequest. getFile (iterator. next (); 12 if (multipartFile! = Null) {13 String fn = multipartFile. getOriginalFilename (); 14 String suffix = fn. substring (fn. lastIndexOf (". "); 15 fileName = dir + Utils. getRandomStringByLength (6) + suffix; 16 String path = realPath + fileName; 17 path = path. replace ("\", "/"); 18 File f = new File (path); 19 if (! F. mkdirs () {20 f. mkdir (); 21} 22 multipartFile. transferTo (f); 23} 24} 25 return fileName; 26} 27 // the above method requires the following method 28 public String getRandomStringByLength (int length) {29 String base = "abcdefghijklmnopqrstuvwxyz0123456789"; 30 Random random = new Random (); 31 StringBuffer sb = new StringBuffer (); 32 for (int I = 0; I <length; I ++) {33 int number = random. nextInt (base. length (); 34 sb. append (base. charAt (number); 35} 36 return sb. toString (); 37}

 

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.