@RequestMapping ("/up")
Public Modelandview up (Multipartfile myfiles, httpservletrequest request) {
Modelandview model = new Modelandview ();
String result = Savepic (request, myfiles); Calling a saved method
if (! Stringutils.isempty (Result) {--Non-essential code
System.out.println ("Upload successful! ");
Model.addobject ("fileName", result);
Model.setviewname ("/picshow");
}else{
System.out.println ("Upload failed! ");
}
return model;
}
Public String savepic (httpservletrequest request,multipartfile multipartfile) {
try {
if (!multipartfile.isempty ()) {
String FilePath = Request.getservletcontext (). Getrealpath ("Uppic"); Get the path to the Uppic folder
String fileName =new Date (). GetTime () +multipartfile.getoriginalfilename (). SUBSTRING ( Multipartfile.getoriginalfilename (). IndexOf ("."), Multipartfile.getoriginalfilename (). Length ());
String newfilename = filepath+file.separator+filename;
File File = new file (NewFileName); To create a file object
if (!file.getparentfile (). exists ()) {//does not exist parent path to create
File.getparentfile (). mkdir ();
}
Multipartfile.transferto (file); For file Dump
return fileName; Returns the name of the stored file
}
} catch (IllegalStateException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return null;
}
SPRINGMVC single-File upload