首页 »Java教程 » struts文件上传:struts构建文件上传( 6) »正文
struts文件上传:struts构建文件上传( 6)
来源: 发布时间:星期四, 2009年1月15日 浏览:33次 评论:0
这是action页面, 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 MaainAction extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { /**@todo: complete the business logic here, this is just a skeleton.*/ MaainForm tainForm = (MaainForm) actionForm; TrainPlanDAO trainPDAO = TrainPlanDAO; Trainplan trainPlan = Trainplan; trainPlan = tainForm.getTrainPlan; FormFile theFile = null; theFile = tainForm.getTheFile1; String p_accessory; p_accessory = theFile.getFileName; try { InputStream stream = theFile.getInputStream; //把文件读入 String filePath = httpServletRequest.getRealPath("/"); //取当前系统路径 ByteArrayOutputStream baos = ByteArrayOutputStream; OutputStream bos = FileOutputStream(filePath + "\\sub" + "/" + theFile.getFileName); //建立个上传文件输出流 //.out.prln(filePath+"/"+file.getFileName); sRead = 0; buffer = [8192]; while ( (sRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, sRead); //将文件写入服务器 } bos.close; stream.close; } catch (Exception e) { .err.pr(e); } try { trainPlan.P_accessory(p_accessory); trainPDAO.addTrainPlan(trainPlan);//数据库插入思路方法 .out.prln("success"); } catch (Exception ex) { } httpServletRequest.Attribute("trainPlain", trainPlan); (actionMapping.findForward("trainplanCreated")); } }
相关文章
读者评论
发表评论
|
|