Action:
1 PackageCom.smics.apps.smicwechat.action.upload;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.File;6 ImportJava.io.FileInputStream;7 ImportJava.io.FileOutputStream;8 Importjava.io.IOException;9 ImportJava.io.InputStream;Ten ImportJava.io.OutputStream; One A Importorg.springframework.util.StringUtils; - - ImportCom.opensymphony.webwork.ServletActionContext; the Importcom.smics.apps.smicwechat.action.BaseAction; - ImportCom.smics.apps.smicwechat.domain.BasicInfo; - - Public classUploadpictureextendsbaseaction { + - Private Static Final intBuffer_size = 16 * 1024; + A PrivateString direction;//point to Page type at - PrivateFile file;//get the uploaded file - PrivateString Filefilename;//get the name of the file - PrivateString Filecontenttype;//get the type of file - - PrivateString Savepath;//directory where files are saved in Private BooleanFlag =false; - to PublicString Forward () { + if("Photo_gallery". Equals (direction)) { - returndirection; the}Else if("Upload_picture". Equals (direction)) { * returndirection; $ }Panax Notoginseng returnERROR; - } the + @Override A PublicString Execute ()throwsException { the //TODO auto-generated Method Stub + //Create a directory file full path based on the server's file save address and source file name -String Dstpath = Servletactioncontext.getservletcontext (). Getrealpath ( This. Getsavepath ()) + "\ \" + This. Getfilefilename (); $String Path = This. Getsavepath () + "\ \" + This. Getfilefilename (); $System.out.println ("Type of uploaded file:" + This. Getfilecontenttype ()); -SYSTEM.OUT.PRINTLN ("Absolute path of uploaded file:" +Dstpath); -SYSTEM.OUT.PRINTLN ("Relative path of uploaded file:" +path); theFile Dstfile =NewFile (dstpath); -Copy This. GetFile (), dstfile);Wuyi This. Setflag (true); the returnSUCCESS; - } Wu - //one of your own packages copy the source file object into the target file object About Private Static voidcopy (file src, file dst) { $InputStream in =NULL; -OutputStream out =NULL; - Try { -in =NewBufferedinputstream (NewFileInputStream (SRC), buffer_size); Aout =NewBufferedoutputstream (NewFileOutputStream (DST), buffer_size); + byte[] buffer =New byte[buffer_size]; the intLen = 0; - while(len = in.read (buffer)) > 0) { $Out.write (buffer, 0, Len); the } the}Catch(Exception e) { the //TODO auto-generated Catch block the e.printstacktrace (); -}finally { in if(NULL!=In ) { the Try { the in.close (); About}Catch(IOException e) { the //TODO auto-generated Catch block the e.printstacktrace (); the } + } - if(NULL!=Out ) { the Try {Bayi out.close (); the}Catch(IOException e) { the //TODO auto-generated Catch block - e.printstacktrace (); - } the } the } the } the - the PublicString Getfilecontenttype () { the returnFilecontenttype; the }94 the PublicString Getfilefilename () { the returnFilefilename; the }98 About Public voidsetfilefilename (String filefilename) { - This. Filefilename =Filefilename;101 }102 103 Public voidSetfilecontenttype (String filecontenttype) {104 This. Filecontenttype =Filecontenttype; the }106 107 PublicString Getsavepath () {108 returnSavepath;109 } the 111 Public voidSetsavepath (String savepath) { the This. Savepath =Savepath;113 } the the PublicString getdirection () { the returndirection;117 }118 119 Public voidsetdirection (String direction) { - This. Direction =direction;121 }122 123 PublicFile GetFile () {124 returnfile; the }126 127 Public voidsetfile (file file) { - This. File =file;129 } the 131 Public BooleanIsflag () { the returnFlag;133 }134 135 Public voidSetflag (Booleanflag) {136 This. Flag =Flag;137 }138 139}
Front page:
1 < type= "file" name= "file">
SSH Simple upload function