SPRINGMVC single-File upload

Source: Internet
Author: User

@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

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.