JS上定义上传按钮:
new Ext.Button({
text : '上传',
iconCls : 'uploadIcon',
handler : function() {
var url = top.rootPath
+ "/public/extjs/mycomp/fileupload/upload.ou?method=uploadfile&linkId=-1";
var dialog = new Ext.ux.UploadDialog.Dialog({
id : 'uploadComp',
url : url,
title : '文件上传__上传过程中关闭窗口,不影响上传',
width : 450,
height : 300,
minWidth : 450,
minHeight : 300,
draggable : true,
resizable : true,
autoCreate : true,
constraintoviewport : true,
modal : true,
post_var_name : 'mms',
reset_on_hide : false,
allow_close_on_upload : true, // 关闭上传窗口是否仍然上传文件
upload_autostart : true
// 是否自动上传文件
})
dialog.show();
//dialog.on('uploadsuccess', successfunc);
}
后台上传功能实现:
private void uploadfile(HttpServletRequest request,
HttpServletResponse response){
try {
String json ="";
String linkId = request.getParameter("linkId");
if(linkId!=null && !linkId.equals("")){
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
List fileList = upload.parseRequest(request);
Iterator iter = fileList.iterator();
while(iter.hasNext()){
FileItem fileItem = (FileItem)iter.next();
if(!fileItem.isFormField()){
String name = fileItem.getName();
String fileSize = new Long(fileItem.getSize()).toString();
if(name == null || name.equals("") || fileSize.equals("0"))
continue;
String fileName = name.substring(name.lastIndexOf("
\\")+1);
//存储文件
String suffix = name.substring(name.lastIndexOf(".")+1);
String uuid = UUID.randomUUID().toString();
// File saveFile = new File("d:\\upload\\"+uuid+"."+suffix);
String staffId = LoginParams.getLoginStaff().getStaffId();
String fileType = getFileType(suffix);
//保存文件
FileSystem hdfs = TThdfsHelper.getFileSystem();
// 上传文件到HDFS集群
String path = TThdfsHelper.getFullPath("flowForm/2013/"+uuid+"."+suffix);
InputStream input = fileItem.getInputStream();
//InputStream input = fileItem; // 输入流,根据实际需要处理
ActionResult uploadresult = hdfs.writeFile(path, input, true, true);
TThdfsHelper.close(hdfs);
FileVO fileVo = new FileVO();
fileVo.setFileName(fileName);
fileVo.setFileSize(fileSize);
fileVo.setSuffix(suffix);
fileVo.setUUID(uuid);
fileVo.setFileType(fileType);
fileVo.setStaffId(staffId);
fileVo.setLinkId(linkId);
int result = (Integer)BOCallProxy.executeBO("com.redsea.file.bo.FileBO",
"uploadFile", new Object[]{fileVo});
if(uploadresult.getCode().equals(ActionResult.SUCCESS)){
json ="{'success':true,'message':'上传成功'}" ;
}else{
json ="{'success':false,'message':'上传失败'}" ;
}
}
}
JspUtil.writerPrint(response, json);
}else{
json ="{success:false,message:'缺少组件编码'}";
JspUtil.writerPrint(response, json);
}
// JspUtil.writerJsonPrint(response, json);
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}