struts2+extjs文件上传完整实现(攻克了上传中的各种问题)
版权声明:本文为博主原创文章。未经博主同意不得转载。 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>
struts2+extjs文件上传完整实现(攻克了上传中的各种问题)的更多相关文章
- Struts2 + uploadify 多文件上传完整的例子!
首先,我这里使用的是 Jquery Uploadify3.2版本号 导入相关的CSS JS <link rel="stylesheet" type=" ...
- Struts2入门(七)——Struts2的文件上传和下载
一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...
- struts2实现文件上传(多文件上传)及下载
一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...
- struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- Struts2之文件上传下载
本篇文章主要介绍如何利用struts2进行文件的上传及下载,同时给出我在编写同时所遇到的一些问题的解决方案. 文件上传 前端页面 <!-- 引入struts标签 --> <%@tag ...
- struts2实现文件上传、多文件上传和文件下载
总结的两个问题,就是struts2上传下载的时候对属性名配置要求非常严格: 第一:上传的时候 private File file; private String fileContentType; pr ...
- Struts2+Uploadify文件上传使用详解
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- Struts2 多文件上传
Struts2多文件上传只需要将 单文件上传中的File变成File[] 即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...
随机推荐
- PIE SDK图像裁剪
1.算法功能简介 图像裁剪的目的是获取选定的影像范围区域.图像裁切工具提供像素范围裁切.矢量裁切.栅格图像裁切和几何图元裁切四种方式. 像素范围裁切是基于像素坐标获取矩形裁切区域的裁切方式:矢量裁切是 ...
- Yarn 包管理工具
已经安装的 yarn add vue vue@2.2.5 yarn add element-ui -S yarn add bootstrap@4.0.0-alpha.6 --save yarn ...
- 谈谈UI设计的6个实用小技巧
从事UI设计的朋友们,肯定知道我们在做UI设计时,其实是可以通过一些小技巧来帮我们设计的界面更加的漂亮.实用.交互性强,用户体验更好.今天的话,上海艾艺在互联网上面搜寻了几个小技巧.在这里跟大家一起来 ...
- docker~run起来之后执行多条命令
最近在搞jenkins pipeline的部署工作,而在对.net core进行部署时不希望安装dotnet sdk,为了移植性更好,打算直接使用aspnetcore的docker镜像,通过docke ...
- [Activator-HelloAkka] Define our Actors
The Actor is the unit of execution in Akka. Actors are object-oriented in the sense that they encaps ...
- NetCDF 共享软件 中文
NetCDF 共享软件 转载 在 Models-3 模式中,使用的数据存取接口称为 I/O API,其实就是 NetCDF 文件格式.而由于我们需要了解 Models-3 输出档案的数据情况,因此 ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- spring 依赖注入总结--为什么官方推荐构造器注入
一 公司小伙伴使用了构造器注入,说是spring的官方推荐.但是,我问了三个问题,他都答不出来,感觉能写篇博文. 官方为什么推荐构造器注入? 构造器注入和属性注入的区别是啥? 你知道有几种注入方式吗? ...
- 深入理解JavaScript系列(32):设计模式之观察者模式
介绍 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态发生变化时就会通知所有的观察者对象,使得它们 ...
- UML建模—EA的使用起步
Enterprise Architect(EA) 是一个功能比较强悍的建模工具. 对于一个软件设计者来说,从需求分析到业务设计.类模型设计.数据库设计到测试.发布.部署等一系列软件设计必须的操作都可以 ...