Springmvc upload picture Save to server change picture name save to database

Source: Internet
Author: User
Tags uuid

@RequestMapping ("Upload")
public void Upload (@RequestParam (value = "File", Required = False) Multipartfile file, HttpServletRequest request,httpser Vletresponse response, Modelmap model) {
System.out.println ("Start");
String filepath=otopropertites.get ("Filepatch");
String path = Request.getsession (). Getservletcontext (). Getrealpath ("/upload");//Picture upload path

String fileName = File.getoriginalfilename ();
String sname = filename.substring (Filename.lastindexof ("."));
String uuid = Uuid.randomuuid (). toString ();
String kzs= ". jpg";//Picture suffix name
if (Sname.equals ("")) {
Kzs=sname;
}
String newfilename = uuid+sname;
SYSTEM.OUT.PRINTLN (path);
File TargetFile = new file (path, newfilename);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
try {
File.transferto (targetfile); Save
} catch (Exception e) {
E.printstacktrace ();
}
String fileurl= Request.getcontextpath () + "/upload/" +newfilename;
System.out.print (Request.getcontextpath ());
Map m=new HashMap ();
M.put ("D", fileUrl);//Picture path
System.out.println ();
if (!sname.equals (". PNG ") &&!sname.equals (". png ")) {
Imageutils.scalewithwidth (path+ "/" +newfilename,600,false);
}

This.responsemsg (response, Object2json.bean2json2 (m));//Turn JSON back to the foreground

}

/**
* Scale by width value, etc.
* @param srcimagefile
* @param scale
* @param flag
*/
Public final static void Scalewithwidth (String srcimagefile,
int Tmpwidth, Boolean flag) {
try {
BufferedImage src = imageio.read (new File (Srcimagefile)); Read in file
int width = src.getwidth (); Get the source map width
int height = src.getheight (); Get the source graph length
float scale =width/tmpwidth;
int ewidth=width;
int eheight=height;
if (flag) {//enlarge
Ewidth = (int) (width * scale);
Eheight = (int) (height * scale);
} else {//Zoom Out
if (width>tmpwidth) {
Ewidth = (int) (Width/scale);
Eheight = (int) (Height/scale);
}
}
Image image = Src.getscaledinstance (Ewidth, Eheight,
Image.scale_default);
BufferedImage tag = new BufferedImage (Ewidth, Eheight,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = tag.getgraphics ();
G.drawimage (image, 0, 0, NULL); Draw a zoomed-out diagram
G.dispose ();
Imageio.write (Tag, "JPEG", new File (Srcimagefile));//output to file stream
} catch (IOException e) {
E.printstacktrace ();
}
}

Springmvc upload picture Save to server change picture name save to database

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.