struts构建文件上传(六)

2016-02-19 15:00 2 1 收藏

下面是个简单易学的struts构建文件上传(六)教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

  这是action页面,

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

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"));
}
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1608577.html

延伸阅读
一、概述 Model就是在对用户请求的整个控制过程中,真正处理用户请求并保存处理结果的对象,在整个过程中,我们一般利用JavaBean来把一些信息保存起来以便在各个对象之间传递。因为在框架中,Model对象是真正处理商业逻辑功能的对象,因此也就是框架中应用需求实现相关性最大的部分。 在Struts的实现里,Model的具体表现形式就是ActionForm...
Model就是在对用户请求的整个控制过程中,真正处理用户请求并保存处理结果的对象,在整个过程中,我们一般利用JavaBean来把一些信息保存起来以便在各个对象之间传递。因为在框架中,Model对象是真处理商业逻辑功能的对象,因此也就是框架中应用需求实现相关性最大的的部分。在Struts的实现里,Model的具体表现形式就是ActionForm对象和与其...
标签: Web开发
使用示例: upload.php ?php include_once "upload.class.php"; if ($Submit != '') {     $fileArr['file'] = $file;     $fileArr['name'] = $file_name;     $fileArr['size'] = $file_size;     $fileArr['type'] = $file_type;    &nbs...
标签: 软件教程
flashfxp是一款很常用的FTP管理工具,也是相对而言比较简单的管理工具。下面小编就为大家介绍一下flashfxp怎么上传文件,希望能帮到大家 1、首先,打开FlashFXP软件 2、点击“站点”按钮,将网站添加到新的站点 3、站点添加好后,添加“连接”图标按钮或者双击flashifxp sites下的用户,连接网站。确认网...
彩云如何上传和批量上传文件?   彩云支持单个文件和批量文件上传。上传单文件最大支持2G,批量上传单次上传总容量不超过2G。彩云暂不支持文件夹上传。 同时新增拖拽上传,可以将本地PC的文件轻松拖动上传到云端对应的文件夹下,让您上传文件变得更简单!

经验教程

706

收藏

100
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部