服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

struts构建文件上传(5)


  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"));
}
}(未完待续)

扫描关注微信公众号