所需jar包
ext界面
{
xtype : 'button',
cls : 'btn-color',
text : "模板下載",
width : 58,
handler : downLoad
},
做一個隱形表單將其提交
function downLoad() {
download_form.submit();
}
//表單
<form action="pub/pob/download.action"
style="MARGIN: 0px; PADDING-LEFT: 0px; WIDTH: 0px; PADDING-RIGHT: 0px; HEIGHT: 0px; PADDING-TOP: 0px"
id="download_form" method="post"
name="download_form">
<INPUT id="fName" value="分支總表Excel表格模板.xls" type="hidden" name="fName">
</form>
struts2的配置
<action name="download" class="com.holley.coms.web.pub.sys.Up_DownLoadAction"
method="excute">
<result name="success" type="stream">
//mime類型 因爲要導出xls類型的
<param name="contentType">application/vnd.ms-excel</param>
//一個獲取流的get方法
<param name="inputName">downLoadFile</param>
//對應的是下載文件的名稱,在類中要有其getset方法
<param name="contentDisposition">attachment;filename="${fileName}"</param>
//寫入大小
<param name="bufferSize">1024</param>
</result>
</action>
//後臺java
private String fileName;
private String inputPath;
private InputStream DownLoadFile;
private File excelFile;
/**
* 下載excel文件
*
* @return
*/
public String excute() {
String fName = getRequest().getParameter("fName");
if (StringUtil.isEmpty(fName)) {
System.out.println("文件不存在");
success = false;
return SUCCESS;
}
success = true;
//從前臺獲取文件名稱,set進去
setFileName(fName);
return SUCCESS;
}
/**
* 獲取指定位置文件流
*
* @return
*/
public InputStream getDownLoadFile() {
return ServletActionContext.getServletContext().getResourceAsStream("/fireFox/" + fileName);
}
文件上傳
//上傳按鈕
{
xtype:'button',
cls : 'btn-color',
text : "導入",
width : 58,
handler: fileUp
},
//上傳彈框
//導入文件
function fileUp(){
var upPanel = Ext.create('Ext.form.Panel',{
title:'',
// width:600,
bodyPadding:10,
// height:400,
items:[{
xtype:'textfield',
allowBlank:false,
fieldLabel:'選擇文件',
inputType:'file',
//這裏的name要和下面的fileName獲取的name屬性要一樣
name:'excelFile'
}],
buttons:[{
text:'上傳',
margin:'1 50 3 10',
handler:function(){
var form = this.up('form').getForm();
var fileName = form.findField('excelFile').getValue();
if(fileName==null||fileName==undefined||fileName==""){
form.reset();
Ext.Msg.alert("注意","請選擇Excel文件!");
return;
}
/* var type = fileName.substring(fileName.indexOf("."),fileName.length);
if(Ext.isEmpty(type)||type!=".xls"){
Ext.Msg.alert("注意","請選擇後綴爲.xls的文件!");
form.reset();
return;
} */
if(form.isValid()){
form.submit({
url:'pub/pob/upload.action',
params : {fileName : fileName},
method:'POST',
waitTitle:'請稍等',
waitMsg:'正在上傳,請稍等...',
success:function(fp,o){
Ext.Msg.alert('信息','文件上傳成功');
upWin.close();
},
failure:function(fp,o){
Ext.Msg.alert('警告','連接失敗');
upWin.close();
}
});
}
}
}]
});
upWin = Ext.create('Ext.window.Window',{
width:400,
height:120,
title:'文件上傳',
modal:true,
layout:'fit',
items:[upPanel]
}).show();
}
struts2的配置文件
<action name="upload" class="com.holley.coms.web.pub.sys.Up_DownLoadAction"
method="upload">
<result name="success" type="json">
<param name="contentType">text/html</param>
</result>
</action>
後臺java代碼
/**
* 上傳
*
* @return
* @throws Exception
*/
public String upload() throws Exception {
//這裏的fileName和前臺的fileName要一樣
String fileName = getRequest().getParameter("fileName");
if(StringUtil.isEmpty(fileName)){
return SUCCESS;
}
String excelPath = "upFile\\" + fileName;
String upPath = getSession().getServletContext().getRealPath("/") + excelPath;
if (excelFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(excelFile));
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(upPath));
byte[] buff = new byte[8192];
for (int len = -1; (len = bis.read(buff)) != -1;) {
bos.write(buff, 0, len);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
returnMsg = "文件上傳失敗";
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
success = false;
return SUCCESS;
} finally {
if (bis != null) {
try {
bis.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
List<List<String>> BranchList = parseExcel(excelPath);
boolean flag = optBranch(BranchList);
returnMsg = "文件上傳成功";
success = true;
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
return SUCCESS;
}