uploadify上传文件
Posted 子衿悠悠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uploadify上传文件相关的知识,希望对你有一定的参考价值。
- 页面部分:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<%@ page import="com.cattsoft.baseplatform.func.sm.entity.SysUser"%>
<html>
<head>
<title>文件管理页面</title>
<link type="text/css" rel="stylesheet" href="<%=baseurl%>js/uploadify/uploadify.css"/>
<script type="text/javascript" src="<%=baseurl%>js/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="<%=baseurl%>js/json2.js"></script>
<script type="text/javascript" src="<%=baseurl %>js_model/sm/file.js"></script>
</head>
</html>
<body>
<div class="cbim_nav">
<ul>
<li>请选择图片:</li>
<li id="more">
<form id="form" method="post">
<span style="color:red;"><s:fielderror/></span>
<input type="hidden" name="filefkuuid" id="filefkuuid"
value="<s:property value="#request.im.filefkuuid"/>" />
</form>
<div id="some_file_queue"></div>
<div id="result"></div>
<input type="file" name="uploadify" id="file1" />
<a href="javascript:$(‘#file1‘).uploadify(‘upload‘,‘*‘)">开始上传</a>
<a href="javascript:$(‘#file1‘).uploadify(‘cancel‘, ‘*‘)">取消上传队列</a>
</li>
<li class="cbim_nav_sc"><a href="javascript:void(0)" onclick="deleteFileBatch()">删除</a></li>
</ul>
<script type="text/javascript">
var filefkuuid=document.getElementById("filefkuuid").value;
$(‘#file1‘).uploadify({
‘uploader‘:‘<%=baseurl%>imorg/ImorgAction!uploadFile.action‘,
‘swf‘:‘<%=baseurl%>js/uploadify/uploadify.swf‘,
‘fileObjName‘:‘uploadify‘,
‘buttonText‘: "上传图片",
‘queueID‘ : ‘some_file_queue‘,
‘formData‘:{‘filefkuuid‘:filefkuuid},
‘auto‘:false,
‘height‘:20,
‘width‘:50,
‘method‘:‘post‘,
‘postData‘ : {},
‘multi‘ : true,
‘removeCompleted‘ : true,
‘removeTimeout‘ : 0.5,
‘requeueErrors‘ : true ,
‘fileSizeLimit‘ :‘1024KB‘,
‘fileTypeDesc‘: "请选择 jpg,png,gif,jpeg.bmp格式的图片",
‘fileTypeExts‘:‘*.jpg;*.gif;*.png;*.jpeg;*.bmp‘,
‘onSelectError‘:function(){
alert(‘请选择 jpg,png,gif,jpeg.bmp格式的图片‘);
},
‘onFallback‘:function(){
alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
},
‘onUploadSuccess‘:function(file,data,response){
$.messager.alert("消息","上传图片成功");
queryFile();
}
});
</script>
</div>
<!-- </form> -->
<div class="cbim_table">
<table id="demo"></table>
<script type="text/javascript">
queryFile();
</script>
</div>
2.</body>后台处理:
private File uploadify;
private String uploadifyFileName;
private String uploadifyContentType;
Set,get方法
/**
* 上传图片
*
* @author tangshuan
* @version 1.0, 2015年9月10日
* @see
* @since 1.0
*/
public String uploadFile() {
//保存上传文件名称
String fileName="";
Properties p = PropertiesLoader.load(SysConstants.File.FILE_MANAGER_PROP_NAME);
String root = p.getProperty(SysConstants.File.FILE_ROOT_PHYSICAL);
String fileRoot=p.getProperty(SysConstants.File.FILE_ROOT);
File files=new File(root);
if(!files.exists()){
files.mkdirs();
}
if(uploadify!=null){
if(uploadify.exists()){
InputStream is=null;
OutputStream os=null;
try {
is = new FileInputStream(uploadify);
//以文件后缀名称建立文件夹
String filePath=root+"/"+getName(uploadifyFileName);
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
fileName=new Date().getTime()+this.uploadifyFileName;
File destFile=new File(filePath,fileName);
os = new FileOutputStream(destFile);
byte[] buffer=new byte[1024];
int length=0;
while((length=is.read(buffer))>0){
os.write(buffer, 0, length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(uploadifyFileName!=null){
imfilemg.setFilename(uploadifyFileName);
String s=getName(uploadifyFileName)+"/"+fileName;
imfilemg.setFilepath(fileRoot+"/"+s);
imfilemg.setFilesuffix(getName(uploadifyFileName));
imfilemg.setTabpkval(filefkuuid);
imfilemg.setTablname("im_org_b");
imfilemg.setTabpkname("业务UUID");
try {
imfilemgbService.insertImFilemgB(imfilemg);
} catch (Exception e) {
e.printStackTrace();
}
}
result="uploadFile";
return "uploadFile";
}
Struts配置文件:
<package name="imorg" namespace="/imorg" extends="default">
<action name="ImorgAction"
class="com.cattsoft.nxii.web.sm.ImorgAction" >
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型
<param name="allowedTypes">
image/bmp,image/png,
image/gif,image/jpeg,image/jpg
</param>
配置允许上传的文件大小 单位字节
<param name="maximumSize">1005242880</param> -->
</interceptor-ref>
<interceptor-ref name="catt.jsonSimpleStack" />
</action>
</package>
以上是关于uploadify上传文件的主要内容,如果未能解决你的问题,请参考以下文章