@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