Springmvc+jquery.uploadify Uploading Files

Source: Internet
Author: User
Tags form post

Objective

Previously with ASP. Mvc+uploadify upload files, recently learned SPRINGMVC, so use springmvc+uploadify to do an upload file demo.

Just started to submit the form form, in the controller action with @requestparam multipartfile file will be able to get upload files information. After I directly use uploadify upload, interface did not make any adjustments, upload the process of reporting http400, the client's request does not meet the requirements of the interface, form Post submitted times the text parameter is in form data mode, Instead of uploadify, the parameter format is the way the request payload, so the interface is rewritten as a multipartservletrequest.

Development environment

SpringMVC4, Uploadify,

If you upload a file, you also need to download commons-fileupload, and also download Common-io, common-logging

Project structure

Normal form Upload
 <form action= /user/index  Span style= "color: #800000;" > " Method="  post   " Enctype="  multipart/form-data   ><input type="  file   name="  file   ><input type="  Span style= "color: #800000;" >submit   value="  upload  /></form> 
@RequestMapping ("Upload") public    @ResponseBody String  upload (@RequestParam multipartfile file) throws IOException {                 String path =request.getsession (). Getservletcontext (). Getrealpath ("Upload");        File File=new file (Path,file.getoriginalfilename ());            File.transferto (file); Save            The file return "/success";     }

  

Uploadify Uploading Files
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

  

Interface

@RequestMapping (value = "/upload", method = requestmethod.post) public @ResponseBody String upload (HttpServletRequest re        Quest, HttpServletResponse Response) {String path =request.getsession (). Getservletcontext (). Getrealpath ("Upload");        Multiparthttpservletrequest multiparthttpservletrequest= (multiparthttpservletrequest) request;        map<string,multipartfile> map = Multiparthttpservletrequest.getfilemap ();        System.out.println ("Path:" +path);        File File=new file (path);        if (!file.exists ()) {file.mkdirs (); } try{for (map.entry<string,multipartfile> Entity:map.entrySet ()) {multipartfile mu                Ltipartfile=entity.getvalue ();                File FF = new file (Path,multipartfile.getoriginalfilename ());            Multipartfile.transferto (FF);        } return "Success";            }catch (Exception e) {e.printstacktrace ();        return "error"; }    }

  

Reference

http://smilease.iteye.com/blog/1693898

Http://www.cnblogs.com/fjsnail/p/3491033.html

Springmvc+jquery.uploadify Uploading Files

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.