struts构建文件上传(六)

政治38

政治38

2016-02-19 15:00

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

  这是action页面,

(本文来源于图老师网站,更多请访问https://m.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://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

struts构建文件上传(六)

编程语言 网络编程
struts构建文件上传(六)

struts构建文件上传(二)

编程语言 网络编程
struts构建文件上传(二)

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

struts构建文件上传(九)

编程语言 网络编程
struts构建文件上传(九)

struts构建文件上传(四)

编程语言 网络编程
struts构建文件上传(四)

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

struts构建文件上传(一)

编程语言 网络编程
struts构建文件上传(一)

struts构建文件上传(八)

编程语言 网络编程
struts构建文件上传(八)

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

cs1.5进不去提示your system reported only

cs1.5进不去提示your system reported only

多标签界面比较压缩包

多标签界面比较压缩包
下拉加载更多内容 ↓