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"));
}
}(未完待续)
闽公网安备 35060202000074号