Struts configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> <constant name= "Struts.devmode" value= "true"/> <constant name= "Struts.ognl.allowStaticMetho Daccess "value=" true "></constant> < PackageName= "Default"extends= "Struts-default" > <action name= "Upload"class= "Com.itheima.action.UploadAction" method= "Upload" > <result>/1.jsp</result> <resul T name= "input" >/upload.jsp</result> <result name= "error" >/error.jsp</result> </a ction> <action name= "Download"class= "Com.itheima.action.UploadAction" method= "Download" > <result type= "Stream" > <param Name= "ContentType" >application/octet-stream</param> <!--using the OGNL expression in the configuration file-- <param name= "contentdisposition" >attachment;filename=${@[email protected] (photofilename, ' UTF-8 ')}</ param> <param name= "InputName" >myInputStream</param> </result> </ Action> </ Package></struts>
Action method:
1 Packagecom.itheima.action;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 ImportJava.io.InputStream;8 ImportJava.io.OutputStream;9 Ten ImportOrg.apache.struts2.ServletActionContext; One A ImportCom.opensymphony.xwork2.ActionSupport; - - Public classUploadactionextendsActionsupport { the PrivateFile photo; - PrivateString Photofilename; - PrivateString Photocontenttype; - PrivateInputStream Myinputstream; + - + PublicInputStream Getmyinputstream () { A returnMyinputstream; at } - - Public voidSetmyinputstream (InputStream myinputstream) { - This. Myinputstream =Myinputstream; - } - in PublicFile Getphoto () { - returnphoto; to } + - Public voidSetphoto (File photo) { the This. Photo =photo; * } $ Panax Notoginseng PublicString Getphotofilename () { - returnPhotofilename; the } + A Public voidsetphotofilename (String photofilename) { the This. Photofilename =Photofilename; + } - $ PublicString Getphotocontenttype () { $ returnPhotocontenttype; - } - the Public voidSetphotocontenttype (String photocontenttype) { - This. Photocontenttype =Photocontenttype;Wuyi } the - PublicString Download ()throwsfilenotfoundexception{ WuPhotofilename = "cosmetic advertising. jpg"; - //is Myinputstream assignment . AboutString storedirectory = Servletactioncontext.getservletcontext (). Getrealpath ("/files/plastic surgery ad. jpg"); $Myinputstream =NewFileInputStream (storedirectory); - - returnSUCCESS; - } A PublicString upload () { + Try { theInputStream in =NewFileInputStream (photo); -String storedirectory = Servletactioncontext.getservletcontext (). Getrealpath ("/files"); $OutputStream out =NewFileOutputStream (storedirectory+ "/" +photofilename); the intLen =-1; the byteB[] =New byte[1024]; the while((Len=in.read (b))!=-1){ theOut.write (b, 0, Len); - } in in.close (); the out.close (); the returnSUCCESS; About}Catch(Exception e) { the e.printstacktrace (); the returnERROR; the } + } -}
JSP page:
<s:form action= "Upload" enctype= "Multipart/form-data" > <s:file label= "Liang Zhao" name= "Photo" ></s:file > <s:submit value= "upload" ></s:submit> </s:form>
Struts File upload Download