二话不说上代码
练习:
view:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>ff</title>
<link rel="stylesheet" type="text/css" href="ext3.0/resources/css/ext-all.css" />
<script type="text/javascript" src="ext3.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext3.0/ext-all.js"></script>
<script type="text/javascript" src="ext3.0/FileUploadField.js"></script>
<style type="text/css">
.special-css{
font-size:9pt;color:red;padding:3px;
}
</style>
<script type="text/javascript">
Ext.onReady(function (){
Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL="ext3.0/resources/images/default/s.gif";
var _form=new Ext.form.FormPanel({
title:'文件上传',
frame:true,
border:false,
height:100,
fileUpload:true,
width:400,
applyTo:'form',
url:'http://localhost:8080/myst/filefile.action',
items:[{
xtype:'textfield',
allowBlank:false,
name:'reportFile',
fieldLabel:'文件上传',
inputType:'file',
id:'fl'
}],
buttons:[{
text:'提交',
handler:function (){
if(_form.getForm().isValid()){
_form.getForm().doAction('submit',{
clientValidation : true,
success:function (form,action){
Ext.Msg.alert('信息',"s");
},
failure:function (form,action){
Ext.Msg.alert('错误','失败');
}
});
}
}
}]
});
});
</script>
</head>
<body>
<div id="form"></div>
</body>
</html>
Action:
public class FileForm extends ActionSupport implements ServletRequestAware , ServletResponseAware{
private File file;
private String fileFileName;
private HttpServletRequest request;
private HttpServletResponse response;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
//读文本文件的。
public String execute() throws Exception {
FileInputStream fi = new FileInputStream(this.getFile());
BufferedReader br = new BufferedReader(new InputStreamReader(fi));
String line ="";
while((line= br.readLine())!=null){
System.out.println(line);
}
br.close();
response.reset();
PrintWriter writer = response.getWriter();
writer.write("{success:true}");
return null;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response= response;
}
/*读Excel文件,也可以将文件里的内容插入到数据库中相应的jar为jxl.jar
public void readExcel(File file) throws Exception{
try {
Workbook book = Workbook.getWorkbook(file);
Sheet se = book.getSheet(1);
int rownum = se.getRows();
for (int i = 1; i < rownum; i++) {
System.out.println(se.getCell(0,i)+","+se.getCell(1,i)+","+se.getCell(2,i));
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
*/
s2:
<action name="fileform" class="com.wys.web.FileForm">
</action>
其它略。。