`

Ext实现文件上传

阅读更多
Ext实现文件上传
最近在项目开发中用到Ext文件上传,在网上找到很多例子,不过大多数例子FormPanel中只包括一个文件上传的组件,表单中并没有包括其它内容,现在分享下包括其它内容的例子,具体步骤如下:
导入包:commons-fileupload.jar commons-io-1.3.2.jar
1、首先在页面中必须将表单设置为:fileUpload:true
   文件选择组件使用:xtype : "fileuploadfield"
frm = new Ext.form.FormPanel({
buttonAlign : 'center',
labelAlign : 'right',
labelWidth : 100,
autoHeight : true,
frame : true,
bodyStyle : 'padding:8px 0 0 0;',
fileUpload : true,// 进行文件上传时必须执行
/*
* defaults: {
*
* allowBlank: false, msgTarget: 'side' },
*/
items : {
columnWidth : .50,
layout : 'column',
items:[{
columnWidth : .85,
layout : 'form',
items : [{
name : "title",
xtype : "textfield",
fieldLabel : "标题",
allowBlank : false,
blankText : "标题不可以为空!",
width : 260
}, {
name : 'content',
xtype : "textarea",
fieldLabel : "内容",
allowBlank : false,
height : 80,
blankText : "内容不可以为空!",
width : 260
}, {
name : 'receivername',
xtype : "textarea",
fieldLabel : "接收人",
height : 80,
width : 260,
//listeners : {
// 'focus' : {
// fn : function(field) {
// win2.show(this);
// }
// }
//},
allowBlank : false,
blankText : "接收人不可以为空!",
scope : this

}, {
xtype : 'textfield',
name : 'receivercode',
id : 'receivercode',
hidden : true,
hideLabel : true
}, {
name : 'appendfile',
xtype : "fileuploadfield",
fieldLabel : "附件",
emptyText : '请选择上传文件',
width : 260
}, {
name : 'remark',
xtype : "textarea",
fieldLabel : "备注",
allowBlank : false,
blankText : "备注不可以为空!",
height : 80,
width : 260
      }]
},{
columnWidth : .15,
layout : 'form',
items : [{
},{
},{
},{
},{
},{
},{
},{
},{
},{
},{
name : 'puritem',
xtype : "button",
text:'选择...',  
handler:function(){
win2.show(this);
},
labelWidth : 300
}]
}]
},
buttons : [{
text : '发送',
handler : submit

}, {
text : '保存',
handler : submitsave
}, {
text : '关闭',
handler : function() {
win.hide();
}

}]

});
2、在后台Action中提取页面数据,需要注意如果现在从request中提取页面数据,这个时候是提取不到数据的,页面中加入了fileUpload:true后,从request中就提取不到数据,需要经过中间一个类进行操作。
中间类:
package com.wfzcx.fasp.sys.message.control;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UpLoadFile {
static UpLoadFile up=new UpLoadFile();
public static UpLoadFile getUpLoadFile() {

return up;
}
public List readRequestInputStream(HttpServletRequest request,long sizeMax) {
DiskFileItemFactory factory=new DiskFileItemFactory();  
ServletFileUpload fu=new ServletFileUpload(factory);
//factory.setSizeThreshold(1024*1024*30);
fu.setSizeMax(sizeMax);
FileItem fi;

List list=new ArrayList();
try {

List list2=fu.parseRequest(request);
uploadList udata=new uploadList();

//for (Object object : list2) {
for(int i=0;i<list2.size();i++){
fi = (FileItem)list2.get(i) ;

String name=fi.getName();
if(name!=null&&fi.getContentType()!=null) {
int index=name.lastIndexOf('\\');
index=index==-1?name.lastIndexOf('/'):index;
    if(index!=-1)
    name=name.substring(index+1);
    udata.setInput(fi.getInputStream());
    udata.setFilesize(fi.getSize());
    udata.setFiletype(fi.getContentType());
    udata.setFileName(name);
list.add(udata);
}else {
udata.put(fi.getFieldName(), fi.getString());

}
}

return list;

} catch (FileUploadException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
return null;
}

public boolean upLoadFile(HttpServletRequest request,long sizeMax,String filepath) {
List list= readRequestInputStream(request, sizeMax);
boolean pand =false;
//for (uploadList stream : list) {
for(int i=0;i<list.size();i++){
uploadList stream = new uploadList();
stream = (uploadList) list.get(i);
pand=writeFile(stream.getInput(), filepath+stream.getFileName());
}
return pand;

}

boolean writeFile(InputStream stream,String filepath){
if(stream==null||filepath==null)
return false;

      try {

          OutputStream bos = new FileOutputStream(filepath);
        
          int bytesRead = 0;
          byte[] buffer = new byte[1024];
          while ( (bytesRead = stream.read(buffer, 0, 1024)) != -1) {
            bos.write(buffer, 0, bytesRead);
          }
          bos.close();
          stream.close();
          return true;
        }catch(Exception e){
          System.err.print(e);
              return false;
        }


}
public class uploadList{
HashMap mapvalue=new HashMap();
public uploadList() {}
public void put(String key,String value) {
mapvalue.put(key, value);
}
public String getmap(String key) {
return (String) mapvalue.get(key);

}
InputStream input=null;
String fileName=null;
long filesize=0l;
String filetype="";
    public uploadList(InputStream input,String fileName,long filesize,String filetype){
this.input = input;
this.fileName = fileName;
this.filesize = filesize;
this.filetype = filetype;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInput() {
return input;
}
public void setInput(InputStream input) {
this.input = input;
}
public long getFilesize() {
return filesize;
}
public void setFilesize(long filesize) {
this.filesize = filesize;
}
public String getFiletype() {
return filetype;
}
public void setFiletype(String filetype) {
this.filetype = filetype;
}
}


}
后台Action:
public class MessageController extends MultiActionController {
public ModelAndView addSaveMessage(HttpServletRequest request,
HttpServletResponse response) {
List filedatas = UpLoadFile.getUpLoadFile().readRequestInputStream(
request, 1024 * 1024 * 500);
uploadList udata = (uploadList) filedatas.get(0);
String title = udata.getmap("title");
String content = udata.getmap("content");
String receivercode = udata.getmap("receivercode");
String receivername = udata.getmap("receivername");
String remark = udata.getmap("remark");
InputStream file = udata.getInput();//上传的文件流
System.out.println("文件名:" + udata.getFileName());
System.out.println("文件大小:" + udata.getFilesize());
System.out.println("文件类型:" + udata.getFiletype());
}


}
3、可以根据业务需求进行数据库操作。
注意在使用本方法的时候可能会出现乱码现象。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics