Struts File Upload

Source: Internet
Author: User

If the file upload is unsuccessful, the default is return "input" <result name= "Success" >/success.jsp</result> <result name= "Input" >/ Index.jsp</result>Single File upload servletcontext context=Servletactioncontext.getservletcontext (); just get Webroot String dir=context.getrealpath ("/files/" +pptfilename);                System.out.println (dir); FileOutputStream Fos=NewFileOutputStream (dir); FileInputStream FIS=NewFileInputStream (PPT); byte[] buffer=New byte[1024]; intLen=0;  while(Len=fis.read (buffer))!=-1) {fos.write (buffer,0, Len);        } fos.close (); Fis.close (); upload limit<interceptors> <interceptor-stack name= "Fileup" > <interceptor-ref name= "defaultstack" > <para M name= "fileupload.maximumsize" >2000</param> <param name= "Fileupload.allowedtypes" >text/plain,text /plain</param> <param name= "fileupload.allowedextensions" >html,dtd,xml</param> </ Interceptor-ref> </interceptor-stack> </interceptors>Image/png,image/bmp,image/jpg<default-interceptor-ref name= "Fileup" ></default-interceptor-ref><s:form action= "S/upload_upload" method= "POST"enctype= "Multipart/form-data" theme= "simple" > <s:fielderror name= "ppt" ></s:fielderror>File:<s:file name= "ppt" label= "file" ></s:file>Pptdesca:<s:textfield name= "pptdesc[0" "label=" Pptdesca "></s:textfield> while(Iterator.hasnext ()) {File File=(File) iterator.next (); String dir=context.getrealpath ("/resource/image/knowledge/upload/" +Caseimgfilename.get (caseimg.)); System.out.println (dir+ "***********dir"); Try{FIS=Newfileinputstream (file); FOS=NewFileOutputStream (dir); byte[] bs=New byte[1024]; intLen=0;  while((Len=fis.read (BS))!=-1) {fos.write (BS,0, Len); }                                    } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("File input Error"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{                    Try{fos.close ();                    Fis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockSYSTEM.OUT.PRINTLN ("input stream, output stream shutdown error"); }                                    }            }
String rpath= "resource/image/knowledge/upload/"; the path stored in the database cannot be an absolute path must be a relative path

Struts File Upload

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.