版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/shanhuhau/article/details/28617999

首先须要引入上传控件

<script type="text/javascript" src="<%=basePath%>/js/ext/examples/ux/fileuploadfield/FileUploadField.js" charset="utf-8"></script>

弹出上传框相应extjs代码


var uploadForm=new Ext.FormPanel({
id:'uploadForm',
width:520,
frame:true,
fileUpload: true,
autoHeight:true,
bodyStyle:'10px 10px 0px 10px',
labelWidth:50,
enctype: 'multipart/form-data',
defaults:{
anchor: '95%',
allowBlank: false
},
items:[
{
xtype:'fileuploadfield',
emptyText: '请选择上传文件...',
fieldLabel: '文件:',
id:'uploadFile',
name: 'upload',
allowBlank: false,
blankText: '文件名不能为空.',
buttonCfg: {
text: '选择...'// 上传文件时的本地查找按钮
}
}
],
buttons: [{
text: '上传',
handler: function(){
if(uploadForm.getForm().isValid()){
uploadForm.getForm().submit({
url:basePath+ '/documentManage/upload_upload.action',
method:'POST',
waitTitle: '请稍后',
waitMsg: '正在上传文档文件 ...',
success: function(fp, action){
Ext.MessageBox.alert('信息', action.result.msg);
Ext.getCmp("uploadFile").reset(); // 指定文件字段的id清空其内容
addwin.hide();
grid.store.load({params:{start : 0,limit : combo.value}});
},
failure: function(fp, action){
Ext.MessageBox.alert('警告', action.result.msg);
addwin.hide();
}
});
}
}
},{
text: '重置',
handler: function(){
uploadForm.getForm().reset();
}
}] }); addwin = new Ext.Window({
title : '上传新文档',
closable : true,
width : 520,
autoHeight: true,
border : false,
plain : true,
modal : true,
layout : 'fit',
bodyStyle : 'padding:5px;',
maximizable : false,// 禁止最大化
closeAction : 'hide',
closable : true,// 是否有关闭
collapsible : true,// 可折叠
iconCls : 'bind',
items : [uploadForm]
});

struts2 action代码

package cn.com.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import cn.com.css.common.action.BaseAction; public class FileUploadAction extends BaseAction { private static final long serialVersionUID = 5156288255337069381L; private String msg;
private String contentType;
private File docmentFile;
private String fileName; public String upload() throws Exception {
String realPath = "E:\\" + fileName;
if (docmentFile.isFile()) {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(docmentFile));
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(realPath));// 为以防万一,以后写文件的路径尽量写成正双斜杠的
// 从源文件里取数据,写到目标文件里
byte[] buff = new byte[8192];
for (int len = -1; (len = bis.read(buff)) != -1;) {
bos.write(buff, 0, len);
}
bos.flush();
} catch (IOException ie) {
ie.printStackTrace();
msg="文件上传失败";
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
return "none";
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
}
msg="文件上传成功";
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
return "none";
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} // since we are using <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>FileName
public String getUploadFileName() {
return fileName;
} public void setUploadFileName(String fileName) {
this.fileName = fileName;
} // since we are using <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>ContentType
public String getUploadContentType() {
return contentType;
} public void setUploadContentType(String contentType) {
this.contentType = contentType;
} // since we are using <s:file name="upload" ... /> the File itself will be
// obtained through getter/setter of <file-tag-name>
public File getUpload() {
return docmentFile;
} public void setUpload(File docmentFile) {
this.docmentFile = docmentFile;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public String getContentType() {
return contentType;
} public void setContentType(String contentType) {
this.contentType = contentType;
} public File getDocmentFile() {
return docmentFile;
} public void setDocmentFile(File docmentFile) {
this.docmentFile = docmentFile;
} }

struts.xml配置:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="documentManage" namespace="/documentManage"
extends="global-struts-default"> <action name="upload_*"
class="cn.com.FileUploadAction"
method="{1}">
<result type="json" name="none">
<param name="contentType">text/html;charset=utf-8</param>
<param name="excludeProperties">
user.myQuestionses,user.messages,user.myNotes,user.taskPapers,
user.tasks,user.testPapers,user.articles
</param>
</result>
</action>
</package>
</struts>

最新文章

  1. CSS魔法堂:重拾Border之——图片作边框
  2. linux 根据文件大小查找文件
  3. UIPickerView滚轮选择器视图
  4. java 22 - 14 JDK1.5以后的Lock锁
  5. Java 获取当前系统时间方法比较
  6. yum_rpm(利用dvd建立本地yum库)
  7. 异常:exception和error的区别
  8. Android下 ionic view 无法登录
  9. mysql 备份与还原
  10. ligerUI路径问题
  11. iOS开动画效果之──实现 pushViewController 默认动画效果
  12. H264解码的一个測试程序
  13. Windows 8实例教程系列 - 数据绑定基础实例
  14. php+ajax+jq
  15. Markdown内嵌Html语言
  16. 认识 SurfaceView
  17. Error:unsupported class file version 52.0问题的解决
  18. eclipse换了高版本的maven插件后报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project
  19. 347. Top K Frequent Elements 最常用的k个元素
  20. NetScaler 10.1的配置以及结合StoreFront的部署

热门文章

  1. PIE SDK专题制图打开模板
  2. Oracle RAC集群搭建(三)--挂载磁盘
  3. vue-scroller的使用 &amp;&amp; 开发自己的 scroll 插件
  4. MongoDB数据库进阶 --- 增删查改...
  5. jquery:字符串(string)转json
  6. kafka配置文件中参数的限制
  7. Java面试题03-访问权限控制
  8. IDEA 14.0 (默认模式) 快捷键
  9. php中的$_GET如何获取带有“#”的参数
  10. [转]C# - JSON详解