Struts upload file

Source: Internet
Author: User

Core code:

* * Generated by myeclipse Struts * Template PATH:TEMPLATES/JAVA/JAVACLASS.VTL * * Package com.ljg.struts.action;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;

Import Org.apache.struts.upload.FormFile;

Import Com.ljg.struts.form.UserForm; public class Registeraction extends Action {public Actionforward execute (actionmapping mapping, actionform form, H Ttpservletrequest request, HttpServletResponse response) {UserForm UserForm = (UserForm) form;//TODO auto-generated
		Method stub String username=userform.getusername ();
		Formfile Formfile=userform.getmyphoto ();
		String Filename=formfile.getfilename ();
		int filesize=formfile.getfilesize (); System.out. println (filename+ "" +filesize);
		InputStream Is=null;
		OutputStream Os=null;
			try {is=formfile.getinputstream ();
			String Keepfilepath=this.getservlet (). Getservletcontext (). Getrealpath ("/file");
			System.out.println ("keepfilepath====" +keepfilepath);
			Os=new fileoutputstream (keepfilepath+ "\" +filename);
			int length=0;
			Byte[] Bytes=new byte[1024];
			while (( (bytes)) >0) {os.write (bytes, 0, length);
		} catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
				Finally {try {is.close ();
			Os.close ();
			catch (Exception E2) {//Todo:handle Exception e2.printstacktrace ();
	} return null; }

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: 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.