flex上传文件
2010-01-15 18:38
281 查看
1.flex端
<mx:Button id="upload" label="上传文件" click="ExternalInterface.call('openUpload');"/>
2后台
2.1 openUpload是后台的js脚本(jsp)
function openUpload(){
var url="/OA/service/upload.jsp";
newWin=jscomNewWinOpenBySize(url,"文件上传",350,150);
}
function jscomNewWinOpenBySize(url,target,width,height){
var tt,w,left,top;
if (!width) width=screen.width;
if (!height) height=screen.height-60;
left=(screen.width-width)/2;
if(left<0){ left=0;}
top=(screen.height-60-height)/2;
if(top<0){ top=0;}
tt="toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes,";
tt=tt+"width="+width+",height="+height+",left="+left+",top="+top;
w=window.open(url,target,tt);
try{
w.focus();
}catch(e){}
return w;
}
upload.jsp
<%@ include file="/service/common.jsp"%>
<%@ page contentType="text/html; charset=gbk" language="java"%>
<html>
<head>
<title>附件上传</title>
<script type="text/javascript" language="javascript">
addEventSimple(window,"load",initial);
function initial(){
o4lukaForm=document.getElementById("lukaForm");
o4myFile1=document.getElementById("myFile1");
o4asslb=document.getElementById("asslb");
o4ahigbg=document.getElementById("ahigbg");
if(o4myFile1.value!=""){
o4lukaForm.submit();
}else{
o4asslb.style.display="block";
}
}
function getFileSize(filePath){
o4asslb=document.getElementById("asslb");
o4asslb.style.display="none";
var image=new Image();
image.dynsrc=filePath;
//alert("你选择的文件大小是:" + image.fileSize/1024 + "K");
o4abiglb=document.getElementById("abiglb");
o4ahigbg=document.getElementById("ahigbg");
o4abiglb.style.display="block";
o4abiglb.innerHTML="你选择的文件大小是:" + image.fileSize/1024 + "K";
o4ahigbg.value=image.fileSize/1024+"K";
}
</script>
</head>
<body>
<table width="100%">
<tr height="30">
<td height="27" colspan="2" align="left" valign="middle">请选择附件(多文件时先压缩)</td>
</tr>
<form name="lukaForm" enctype="multipart/form-data" action="<%=webAppPath%>/uploadFiles.do" method="post">
<input type="hidden" name="method" value="toupload">
<tr height="30">
<td height="20" colspan="2" align="left" valign="middle"><label id="asslb" style="display:none; width:auto;"><font color="#FF0000">附件为空</font></label><label id="abiglb" style="display:none; width:auto; font-size:12px; color:#009900"></label><input type="hidden" id="ahigbg" name="ahigbg" value=""/></td>
</tr>
<tr height="30">
<td height="27" align="left" valign="middle"><input type="file" name="myFile1" class="input" style="width:90%" onChange="getFileSize(this.value)"/></td>
</tr>
</form>
<tr>
<td height="27" colspan="5" align="center"><input type="submit" onClick="initial();" value="上 传"></td>
</tr>
</table>
</body>
</html>
2.2 交互代码(action)
/**
* 文件上传
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward toupload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.info("文件开始上传---EnterUploadFilesAction(toupload)-----------");
UploadOneActionForm uploadForm = (UploadOneActionForm) form;
ServletContext servletContext = this.getServlet().getServletContext();
UploadTool uploadTool = new UploadTool();
UploadFiles condition = null;
// 文件所有者
UserSession userSession = (UserSession) request.getSession()
.getAttribute("userSession");
String loadowner = userSession.getUsername();
// 文件的大小
String loadsize = request.getParameter("ahigbg");
log.info("文 件 大 小 为:================" + loadsize);
boolean isMyFile = false;
boolean exit = false;
String[] fileResult = new String[2];
FormFile myfile = uploadForm.getMyFile1();
if (myfile != null && myfile.getFileName().length() > 0
&& myfile.getFileSize() < 30 * 1000 * 1000) {
log.info("Enter isMyFile = true");
isMyFile = true;
}
if (isMyFile)
// fileResult = uploadTool.upload4web(servletContext, myfile,
// UploadTool.DIS_UP);
fileResult = uploadTool.uploadFiles(servletContext, myfile,
UploadTool.DIS_UP);
if (isMyFile) {
String filename = fileResult[0];
String filepath = fileResult[1];
log.info("filename: " + filename);
log.info("filepath" + filepath);
// 所有的文件
UploadFiles uploadfiles = uploadService
.getFilesByloadname(filename);
if (uploadfiles == null) {
request.setAttribute("msg", "你所选择的文件 上 传 成 功");
String path = this.getServlet().getServletContext()
.getRealPath("/")
+ UploadTool.UPLOAD_BASE + UploadTool.DIS_UP + filename;
log.info("path:==================" + path);
UploadFiles item = null;
item = new UploadFiles();
item.setLoadname(filename);
item.setLoadflag("F");
item.setLoadowner(loadowner);
item.setLoadsize(loadsize);
item.setLoadtime(LukaTool.date(2));
item.setLoadpath(path);
condition = new UploadFiles();
condition = this.uploadService.uploadFile(item);
log.info("文件上传成功===================");
}else{
request.setAttribute("msg", "该文件存在请更换文件名");
}
}
return mapping.findForward("loadfiles");
}
文件上传的通用类
/**
* 上传文件
*
* @param context
* @param file
* @param module
* @return
*/
public String[] uploadFiles(ServletContext context, FormFile file,
String module) {
String[] result = new String[2];
result[0] = file.getFileName();
result[1] = file.getFileName();
// 准备路径及文件名
StringBuffer sb = new StringBuffer();
sb.append(context.getRealPath("/"));
sb.append(UploadTool.UPLOAD_BASE);
sb.append(module);
sb.append("/");
sb.append(result[1]);
InputStream in;
try {
in = file.getInputStream();
OutputStream out = new FileOutputStream(sb.toString());
// now write data into disk
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
// donot forget to close the streams : )
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
<mx:Button id="upload" label="上传文件" click="ExternalInterface.call('openUpload');"/>
2后台
2.1 openUpload是后台的js脚本(jsp)
function openUpload(){
var url="/OA/service/upload.jsp";
newWin=jscomNewWinOpenBySize(url,"文件上传",350,150);
}
function jscomNewWinOpenBySize(url,target,width,height){
var tt,w,left,top;
if (!width) width=screen.width;
if (!height) height=screen.height-60;
left=(screen.width-width)/2;
if(left<0){ left=0;}
top=(screen.height-60-height)/2;
if(top<0){ top=0;}
tt="toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes,";
tt=tt+"width="+width+",height="+height+",left="+left+",top="+top;
w=window.open(url,target,tt);
try{
w.focus();
}catch(e){}
return w;
}
upload.jsp
<%@ include file="/service/common.jsp"%>
<%@ page contentType="text/html; charset=gbk" language="java"%>
<html>
<head>
<title>附件上传</title>
<script type="text/javascript" language="javascript">
addEventSimple(window,"load",initial);
function initial(){
o4lukaForm=document.getElementById("lukaForm");
o4myFile1=document.getElementById("myFile1");
o4asslb=document.getElementById("asslb");
o4ahigbg=document.getElementById("ahigbg");
if(o4myFile1.value!=""){
o4lukaForm.submit();
}else{
o4asslb.style.display="block";
}
}
function getFileSize(filePath){
o4asslb=document.getElementById("asslb");
o4asslb.style.display="none";
var image=new Image();
image.dynsrc=filePath;
//alert("你选择的文件大小是:" + image.fileSize/1024 + "K");
o4abiglb=document.getElementById("abiglb");
o4ahigbg=document.getElementById("ahigbg");
o4abiglb.style.display="block";
o4abiglb.innerHTML="你选择的文件大小是:" + image.fileSize/1024 + "K";
o4ahigbg.value=image.fileSize/1024+"K";
}
</script>
</head>
<body>
<table width="100%">
<tr height="30">
<td height="27" colspan="2" align="left" valign="middle">请选择附件(多文件时先压缩)</td>
</tr>
<form name="lukaForm" enctype="multipart/form-data" action="<%=webAppPath%>/uploadFiles.do" method="post">
<input type="hidden" name="method" value="toupload">
<tr height="30">
<td height="20" colspan="2" align="left" valign="middle"><label id="asslb" style="display:none; width:auto;"><font color="#FF0000">附件为空</font></label><label id="abiglb" style="display:none; width:auto; font-size:12px; color:#009900"></label><input type="hidden" id="ahigbg" name="ahigbg" value=""/></td>
</tr>
<tr height="30">
<td height="27" align="left" valign="middle"><input type="file" name="myFile1" class="input" style="width:90%" onChange="getFileSize(this.value)"/></td>
</tr>
</form>
<tr>
<td height="27" colspan="5" align="center"><input type="submit" onClick="initial();" value="上 传"></td>
</tr>
</table>
</body>
</html>
2.2 交互代码(action)
/**
* 文件上传
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward toupload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.info("文件开始上传---EnterUploadFilesAction(toupload)-----------");
UploadOneActionForm uploadForm = (UploadOneActionForm) form;
ServletContext servletContext = this.getServlet().getServletContext();
UploadTool uploadTool = new UploadTool();
UploadFiles condition = null;
// 文件所有者
UserSession userSession = (UserSession) request.getSession()
.getAttribute("userSession");
String loadowner = userSession.getUsername();
// 文件的大小
String loadsize = request.getParameter("ahigbg");
log.info("文 件 大 小 为:================" + loadsize);
boolean isMyFile = false;
boolean exit = false;
String[] fileResult = new String[2];
FormFile myfile = uploadForm.getMyFile1();
if (myfile != null && myfile.getFileName().length() > 0
&& myfile.getFileSize() < 30 * 1000 * 1000) {
log.info("Enter isMyFile = true");
isMyFile = true;
}
if (isMyFile)
// fileResult = uploadTool.upload4web(servletContext, myfile,
// UploadTool.DIS_UP);
fileResult = uploadTool.uploadFiles(servletContext, myfile,
UploadTool.DIS_UP);
if (isMyFile) {
String filename = fileResult[0];
String filepath = fileResult[1];
log.info("filename: " + filename);
log.info("filepath" + filepath);
// 所有的文件
UploadFiles uploadfiles = uploadService
.getFilesByloadname(filename);
if (uploadfiles == null) {
request.setAttribute("msg", "你所选择的文件 上 传 成 功");
String path = this.getServlet().getServletContext()
.getRealPath("/")
+ UploadTool.UPLOAD_BASE + UploadTool.DIS_UP + filename;
log.info("path:==================" + path);
UploadFiles item = null;
item = new UploadFiles();
item.setLoadname(filename);
item.setLoadflag("F");
item.setLoadowner(loadowner);
item.setLoadsize(loadsize);
item.setLoadtime(LukaTool.date(2));
item.setLoadpath(path);
condition = new UploadFiles();
condition = this.uploadService.uploadFile(item);
log.info("文件上传成功===================");
}else{
request.setAttribute("msg", "该文件存在请更换文件名");
}
}
return mapping.findForward("loadfiles");
}
文件上传的通用类
/**
* 上传文件
*
* @param context
* @param file
* @param module
* @return
*/
public String[] uploadFiles(ServletContext context, FormFile file,
String module) {
String[] result = new String[2];
result[0] = file.getFileName();
result[1] = file.getFileName();
// 准备路径及文件名
StringBuffer sb = new StringBuffer();
sb.append(context.getRealPath("/"));
sb.append(UploadTool.UPLOAD_BASE);
sb.append(module);
sb.append("/");
sb.append(result[1]);
InputStream in;
try {
in = file.getInputStream();
OutputStream out = new FileOutputStream(sb.toString());
// now write data into disk
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
// donot forget to close the streams : )
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
相关文章推荐
- Flex文件上传
- Flex 带进度条的文件上传
- Flex2.0文件上传功能(Flex2.0正式版)
- Flex文件上传下载
- Flex多文件上传(转)
- Flex多文件上传(转)
- Flex中的文件上传与下载
- 【心得】Flex中的文件上传与下载
- [原创 flex] flex制作的多文件上传组件
- Flex中实现文件上传时的类型过滤
- Flex中的文件上传与下载
- flex,srvlet上传文件,中文乱码
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- flex 解决上传文件中文名字问题
- Flex上传文件功能
- flex上传多个文件
- flex与C#交互实现文件上传
- flex多文件带进度条上传
- Flex +java 多文件上传带进度条