struts构建文件上传(5)

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

struts构建文件上传(5)

这是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"));

}

}(未完待续)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航