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>