现在的位置: 首页 > 综合 > 正文

ext2.0文件上传

2018年03月23日 ⁄ 综合 ⁄ 共 3434字 ⁄ 字号 评论关闭
Ext.onReady(function(){
var form = new Ext.form.FormPanel({
labelAlign: 'right',
title: 'form',
labelWidth: 50,
frame:true,
fileUpload: true,
url: 'student?action=load',//fileUploadServlet
width: 380,

items: [{
xtype: 'textfield',
fieldLabel: '文本框',
name: 'file',
inputType: 'file'//文件类型
}],
buttons: [{
text: '按钮',
handler: function() {
form.getForm().submit({
success: function(form, action){
Ext.Msg.alert('信息', action.result.msg);
},
failure: function(){
Ext.Msg.alert('错误', '失败');
}
});
}
}]
});
form.render("form");
});

而后action中调用通用上传方法

public void upLoad(HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String name = null;
String value = null;
boolean fileFlag = false;
// 文件存放目录
String TMP_DIR = "D://";
File tmpFile = null;
// file name
String fName = null;

FileOutputStream baos = null;
BufferedOutputStream bos = null;
Hashtable<String, ArrayList<String>> paramHt = new Hashtable<String, ArrayList<String>>();
int BUFSIZE = 1024 * 8;
int rtnPos = 0;
byte[] buffs = new byte[BUFSIZE * 8];
// 得到请求类型
String contentType = request.getContentType();
int index = contentType.indexOf("boundary=");
String boundary = "--" + contentType.substring(index + 9);
String endBoundary = boundary + "--";
ServletInputStream sis = request.getInputStream();
System.out.println();
// 循环读取文件
while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
String strBuff = new String(buffs, 0, rtnPos);
if (strBuff.startsWith(boundary)) {

if (name != null && name.trim().length() > 0) {

if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList arrayList = (ArrayList) obj;
al = arrayList;
}
al.add(value);
paramHt.put(name, al);
}
}
name = new String();
value = new String();
fileFlag = false;
rtnPos = sis.readLine(buffs, 0, buffs.length);
if (rtnPos != -1) {

strBuff = new String(buffs, 0, rtnPos);
if (strBuff.toLowerCase().startsWith(
"content-disposition: form-data; ")) {
int nIndex = strBuff.toLowerCase().indexOf("name=/"");
int nLastIndex = strBuff.toLowerCase().indexOf("/"",
nIndex + 6);
name = strBuff.substring(nIndex + 6, nLastIndex);
}
int fIndex = strBuff.toLowerCase().indexOf("filename=/"");
if (fIndex != -1) {
fileFlag = true;
int fLastIndex = strBuff.toLowerCase().indexOf("/"",
fIndex + 10);
fName = strBuff.substring(fIndex + 10, fLastIndex);
fIndex = fName.lastIndexOf("//");
if (fIndex == -1) {
fIndex = fName.lastIndexOf("/");
if (fIndex != -1) {
fName = fName.substring(fIndex + 1);
}
} else {
fName = fName.substring(fIndex + 1);
}
if (fName == null || fName.trim().length() == 0) {
fileFlag = false;
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
continue;
}
}
sis.readLine(buffs, 0, buffs.length);
sis.readLine(buffs, 0, buffs.length);
}
} else if (strBuff.startsWith(endBoundary)) {
System.out.println("2...");
if (name != null && name.trim().length() > 0) {
System.out.println("(name!=null)...");
if (fileFlag) {
bos.flush();
baos.close();
bos.close();
baos = null;
bos = null;
} else {
Object obj = paramHt.get(name);
ArrayList<String> al = null;
if (obj == null) {
al = new ArrayList<String>();
} else {
ArrayList arrayList = (ArrayList) obj;
al = arrayList;
}
al.add(value);

paramHt.put(name, al);
}
}
} else {
if (fileFlag) {
if (baos == null && bos == null) {
tmpFile = new File(TMP_DIR + fName);
baos = new FileOutputStream(tmpFile);
bos = new BufferedOutputStream(baos);
}
bos.write(buffs, 0, rtnPos);
baos.flush();
} else {
value = value + strBuff;
}
}
}

/*
* 有个问题?请求发来的时候...由于是UTF-8的编码;导致上传后的文件名是乱码
*/
System.out.println("上传文件名:" + tmpFile.getName() + " 文件大小:"
+ tmpFile.length() + " 文件路径:" + tmpFile.getPath());
response.getWriter().print("{success:true,msg:'成功'}");
}  

抱歉!评论已关闭.