Springmvc multipartfile File Upload

Source: Internet
Author: User
Tags current time file size file upload json

About SPRINGMVC multipartfile File upload problem, I put the code here


JSP page


Control layer

@RequestMapping ("/edit")
@ResponseBody public Json edit (tgoodcategory goodcategory,httpservletrequest request,@ Requestparam (value= "file", Required=false) multipartfile file) {
	if (file!=null) {
		String s= Uploadfile.loadpicture (file, "CPSM", request);
		Goodcategory.setinstruction ("file/" +s);
	JSON j = new JSON ();
	try{
		j.setsuccess (true);
		J.setobj (Goodcategoryservice.edit (goodcategory));
		J.setmsg ("Edit succeeded!");
	} catch (Exception ex) {
		ex.printstacktrace ();
		J.setmsg ("Edit failed!");
	}
	return j;

}

File Upload Tool class

/** * Files Upload * @param file foreground file * @param name Store name prefix * @param request HttpServletRequest * @return Database storage field Name: name+ Current Time */
	public static string LoadPicture (Multipartfile file, string Name,httpservletrequest request) {string s= "";
	SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss");
		if (!file.isempty ()) {byte[] bytes;

			try {bytes = File.getbytes ();
			String RootPath = Request.getsession (). Getservletcontext (). Getrealpath ("file");
				
				
			File dir = new file (RootPath + file.separator);
			if (!dir.exists ()) {dir.mkdirs ();
			}//string Path=dir.getabsolutepath () + File.separator + file.getoriginalfilename ();
			String[] T=file.getoriginalfilename (). split ("\ \");
			String Picturetype=t[t.length-1]; String Picturename=name+sdf.format (New Date ()) + "."
			+picturetype;
			String Path=dir.getabsolutepath () + File.separator +picturename;
				
			S=picturename;
			File Serverfile = new file (path);
			Bufferedoutputstream stream; stream = new BufferedoutPutstream (New FileOutputStream (Serverfile));
			Stream.Write (bytes);
			Stream.Close ();
			return picturename; } catch (Exception e) {//TODO auto-generated catch block System.out.println ("Fileload Error ...:" +e.getmessage ())
			;
}} return S; }



Configuration file for Springmvc

<bean id= "Multipartresolver"  
        class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >  
        <!--default encoding--
        <property name= "defaultencoding" value= "Utf-8"/>  
        <!--file size max--
        <property name= "maxuploadsize" value= "10485760000"/>  
        <!--max in memory--
        <property name= " Maxinmemorysize "value=" 40960 "/>  
</bean>





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.