Struts build File Upload (vi)

Source: Internet
Author: User

This is the action page,

Package Tester.business.maitain
Import tclcc.tester.util.Selector;
Import org.apache.struts.action.*;
Import javax.servlet.http.*;
Import java.util.*;
Import Org.apache.struts.upload.FormFile;
Import java.io.*;
public class Maintainaction
extends Action {
Public actionforward execute (actionmapping actionmapping,
A Ctionform actionform,
HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse) {
/ * * @todo: Complete the business logic here, this is just a skeleton.*/
maintainform maintainform = (maintainform) actio Nform;
Trainplandao Trainpdao = new Trainplandao ();
Trainplan Trainplan = new Trainplan ();
Trainplan = Maintainform.gettrainplan ();
Formfile thefile = null;
Thefile = Maintainform.getthefile1 ();
String p_accessory;
P_accessory = Thefile.getfilename ();
try {
InputStream stream = Thefile.getinputstream ();//Read the file into the
String FilePath = Httpservletrequest.getreaLpath ("/"); Takes the current system path
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
OutputStream BOS = new FileOutputStream (FilePath + "\\sub" + "/" +
Thefile.getfilename ());//create an output stream for an uploaded file
//system . OUT.PRINTLN (filepath+ "/" +file.getfilename ());
int bytesread = 0;
byte[] buffer = new byte[8192];
while (bytesread = stream.read (buffer, 0, 8192))!=-1) {
Bos.write (buffer, 0, bytesread);//write file to server
}
Bos.close ();
Stream.Close ();
} catch (Exception e) {
System.err.print (e);
}
try {
Trainplan.setp_accessory (p_accessory);
Trainpdao.addtrainplan (Trainplan);//Call database Insert method
SYSTEM.OUT.PRINTLN ("Success");
} catch (Exception ex) {
}
Httpservletrequest.setattribute ("Trainplain", Trainplan);
Return ( Actionmapping.findforward ("trainplancreated"));
}
}

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.