Recently free to work, on the collation of commonly used functions, the following is the project image upload function, upload the image to the server tomcat, the database to store the relative path, we learn together.
Environment: Eclipse jdk1.7 TOMCAT7 maven3.5
Steps:
@RequestMapping (params= "uploadpicture") @ResponseBody Public string addfilemsgpic (multipartfile file,httpservletrequest request) throws Exception{ string path= ". /manage_ssm/uploadpicture "; //Create file file dir=new file (path ); if (!dir.exists ()) { dir.mkdirs (); } string username= (String) request.getsession (). getattribute ("UserName"); // Back to the original file system on the clientThe file name string filename=file.getoriginalfilename (); //username+ suffix name string img=username+filename.substring (Filename.lastindexof (".")); /zhao.jpg fileoutputstream imgout=new FileOutputStream (New file (dir,img));//Create a new File instance from the dir abstract path name and img pathname string. imgout.write ( File.getbytes ());//Returns the contents of a byte array file imgout.close (); Map<String, String> map=new HashMap<String, String> (); string rpath = path+ "/" +img; &nbsP; map.put ("RPath", RPath); jsonobject Jsonobject = jsonobject.fromobject (map);//Convert a JSON string to a JSON object string r=jsonobject.tostring (); string s= Urlencoder.encode (r, "Utf-8");//Encryption return s; }
@RequestMapping (params= "Picturetodb") @ Responsebody public int picturetodb (string src,httpservletrequest Request) { string newsrc = src.replace (" ", "+"); int flag=connorcl.conntotable (newsrc,request); if (flag==1) return 1; else return 2; }
public class connorcl { private static connection conn; private static statement stat; private static string driver = "Oracle.jdbc.driver.OracleDriver"; private static int SUCCESS=1; private static int FILE=2; public static int conntotable (string file,httpservletrequest Request) { try { //Database Connection Class.forName (Driver); conn = java.sql.drivermanager.getconnection ("jdbc:oracle:thin:@ 127.0.0.1:1521:ORCL ", " GKBMFZXT ", " GKBMFZXT "); stat = conn.createstatement (); string username= (String) request.getsession (). getattribute ("UserName"); &nbsP; string sql= "Update user_ Table set picture= ' "+file+" ' where user_name= ' "+username+" ' "; Stat.execute (Sql.tostring ()); return SUCCESS; } catch ( Classnotfoundexception e) { e.printstacktrace (); } catch (sqlexception e) { e.printstacktrace (); } finally { if (Null != stat) { try { stat.close (); } catch (SQLException e) { e.printstacktrace (); } } if (null ! = conn) { try { conn.close (); } catch (sqlexception e) { e.printstacktrace (); } } } return FILE; } }
<script src= "Js/ajaxfileupload.js" ></script><script type= "Text/javascript" > Function upload () { var file= "file"; var picelement=document.getelementbyid (file) .value; Var picformat=picelement.substring (Picelement.lastindexof ('. ') +1,picelement.length) if (picformat!= "JPG") { alert ("Please select file in jpg format!"); return; } $. Ajaxfileupload ({ url: "User.do? Uploadpicture ", secureuri: false, fileElementId:file, datatype: ' JSON ', success:function (data) { Data=decodeuricomponent (data);//Decode var arr=json.parse (data);//json.parse () method parse a JSON string $ ("#fsrc"). Val (Arr.rpath); $.post ( "user.do?picturetodb&src=" +$ ("#fsrc"). Val (), function (data) { //data=decodeuricomponent (data); //var arr =json.parse (data); if (data==1) { alert ("Upload successful! "); }else{ alert ("Upload failed! "); } } ); }, error: function () { alert ("Upload failed!" The current time is: "+new date (). tolocaletimestring ()); } });}; Function findfile () { document.getelementbyid ("file"). Click ();
Java image upload