struts构建文件上传(六)


关键词

struts构建文件上传(六)

摘要


这是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 class MaintainAction
   extends Action {
 public ActionForward execute(ActionMapping actionMapping,
                              ActionForm actionForm,
                              HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse) {
   /**@todo: complete the business logic here, this is just a skeleton.*/
   MaintainForm maintainForm = (MaintainForm) actionForm;
   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 {


这是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 class MaintainAction
   extends Action {
 public ActionForward execute(ActionMapping actionMapping,
                              ActionForm actionForm,
                              HttpServletRequest httpServletRequest,
                              HttpServletResponse httpServletResponse) {
   /**@todo: complete the business logic here, this is just a skeleton.*/
   MaintainForm maintainForm = (MaintainForm) actionForm;
   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(); //把文件读入
     String filePath = httpServletRequest.getRealPath("/"); //取当前系统路径
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     OutputStream bos = new FileOutputStream(filePath + "sub" + "/" +
                                             theFile.getFileName()); //建立一个上传文件的输出流
     //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); //将文件写入服务器
     }
     bos.close();
     stream.close();
   } catch (Exception e) {
     System.err.print(e);
   }

   try {
     trainPlan.setP_accessory(p_accessory);
     trainPDAO.addTrainPlan(trainPlan);//调用数据库插入方法
     System.out.println("success");

   } catch (Exception ex) {
   }
   httpServletRequest.setAttribute("trainPlain", trainPlan);
   return (actionMapping.findForward("trainplanCreated"));
 }
}
                                       



 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权

本文属于“洪哥笔记”原创文章,转载请注明来源地址:struts构建文件上传(六):http://www.splaybow.com/post/strutsJSP6.html

如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:115085382!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« struts构建文件上传(四) struts构建文件上传(七) »

相关文章:

JSP动态包含  (2014/6/28 8:32:24)

JSP静态包含  (2014/6/27 8:24:43)

Java参数化查询无法得到可更新的记录集  (2014/6/26 9:29:00)

jsp包含文件与被包含文件的contentType必须完全一致  (2014/6/25 9:19:33)

Java可以定义元素个数为0的数组  (2014/6/23 11:24:23)

Java中XML的构造与输出  (2014/3/29 11:51:08)

Java中如何把整形int转换成字符串String类型  (2014/3/28 11:31:28)

自动添加超链接  (2014/1/17 9:43:23)

使用keytool生成私钥  (2014/1/2 20:58:48)

Java递归算法示例  (2013/12/29 19:19:50)