Struts File upload Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.