The basic idea is to configure the path and then upload the image to the directory with the Java I/O API.
String Photopath =
Servletactioncontext.getservletcontext (). Getrealpath ("/user/photo/" + username);
This is the get directory under the server +username
and username is obtained through the session.
This is an image upload action method:
PublicString Execute ()throwsException {//Get usernameMap session =Servletactioncontext.getcontext (). GetSession (); String username= (String) session.get ("username"); //Create an input streamInputStream is =NewFileInputStream (MyFile); //settings File Save directoryString Photopath =Servletactioncontext.getservletcontext (). Getrealpath ("/user/photo/" +username); File Filephotopath=NewFile (Photopath); if(!filephotopath.isdirectory ()) {Filephotopath.mkdir (); } //troubleshoot Chinese file name problemsString Extension = filenameutils.getextension ( This. Getmyfilefilename ()); String filename= Uuid.randomuuid (). toString () + "." +extension; //set the destination fileFile ToFile =NewFile (photopath,filename); //using the output stream to wrap the target fileOutputStream OS =NewFileOutputStream (ToFile); byte[] buffer =New byte[1024]; intLength = 0; while(length = is.read (buffer)) > 0) {os.write (buffer,0, length); } //close the input streamIs.close (); //turn off the output streamOs.close (); return This. SUCCESS; }
Java Web Development image upload function