Struts2+uploadify多文件上傳實(shí)例
本文實(shí)例為大家分享了Struts2+uploadify多文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
首先我這里使用的是 Jquery Uploadify3.2的版本
導(dǎo)入相關(guān)的CSS JS
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/uploadify/uploadify.css" rel="external nofollow" > <script src="<%=basePath%>js/jquery.min.js"></script> <script src="<%=basePath%>js/uploadify/jquery.uploadify.min.js"></script>
接下來是 上傳的 JSP 頁面代碼
<form action="" method="post" >
<input type="file" name="img_file" id="img_file">
<div id="uploadfileQueue" ></div>
<div id="imgs" ></div>
<div id="dialog-message" ></div>
</form>
<p>
<a href="javascript:void(0);" rel="external nofollow" onclick="myUpload()">上傳</a>
<a href="javascript:$('#img_file').uploadify('cancel')" rel="external nofollow" >取消上傳</a>
</p>
這里是最關(guān)鍵的JS 代碼,有注釋
$(function(){
$("#img_file").uploadify({
auto:false,//是否自動(dòng)上傳
height: 30,
buttonText:'選擇圖片',
cancelImage:'<%=basePath%>img/uploadify/uploadify-cancel.png',
swf : '<%=basePath %>js/uploadify/uploadify.swf',
// expressInstall:'js/uploadify/expressInstall.swf',
uploader : '<%=basePath%>json/fileUploadAction.action', //后臺(tái)處理上傳文件的action
width : 120 ,
'multi': true, //設(shè)置為true將允許多文件上傳
'filesSelected': '4',
queueID:'uploadfileQueue',
fileObjName:'img_file', //與后臺(tái)Action中file屬性一樣
/*
formData:{ //附帶值
'userid':'111',
'username':'tom',
'rnd':'111'
},
*/
fileTypeDesc:'上傳文件支持的文件格式:jpg,jpge,gif,png',
fileTypeExts:'*.jpg;*.jpge;*.gif;*.png',//*.jpg;*.jpge;*.gif;*.png
queueSizeLimit : 4,//只能一次上傳4張圖片
// simUploadLimit:1,//一次可以上傳1個(gè)文件
fileSizeLimit:'2097152',//上傳文件最大值 單位為字節(jié) 2M
//返回一個(gè)錯(cuò)誤,選擇文件的時(shí)候觸發(fā)
onSelectError:function(file, errorCode, errorMsg){
switch(errorCode) {
case -100:
alert("上傳的文件數(shù)量已經(jīng)超出系統(tǒng)限制的4個(gè)文件!");
break;
case -110:
alert("文件 ["+file.name+"] 大小超出系統(tǒng)限制的2M大小!");
break;
case -120:
alert("文件 ["+file.name+"] 大小異常!");
break;
case -130:
alert("文件 ["+file.name+"] 類型不正確!");
break;
}
}, //
//上傳到服務(wù)器,服務(wù)器返回相應(yīng)信息到data里
onUploadSuccess:function(file, data, response){
var objs = eval('('+data+')');
var phtml = "<span><img style='width:150;height:150' src='/uploads/"+objs.filename+"'></span>";
if($("#imgs span").length==0){
$("#imgs").html(phtml);
}else{
$("#imgs span:last").after(phtml);
}
},
onSelect : function(file) {
//alert(file.name);
},
//removeCompleted:true,//上傳的文件進(jìn)度條是否消失
requeueErrors:false,
// removeTimeout:2,//進(jìn)度條消失的時(shí)間,默認(rèn)為3
progressData:"percentage",//顯示上傳的百分比
onUploadError : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) { //這里是取消的時(shí)候發(fā)生
// $("#dialog-message").html(errorString);
}
});
});
//上傳文件
function myUpload(){
$("#img_file").uploadify('upload','*');
}
java 上傳的Action 代碼
/**
* 上傳文件的Action
* @author wzh
*
*/
@Controller
@Scope("prototype")
public class FileUploadAction extends BaseAction {
private File img_file;
private String img_fileContentType;//格式同上"fileName"+ContentType
private String img_fileFileName;//格式同上"fileName"+FileName
private String savePath;//文件上傳后保存的路徑
private Map<String, Object> dataMap = new HashMap<String, Object>();
@Override
/***
* 上傳文件
*/
public String execute() throws Exception{
System.out.println("savePath"+getSavePath());
File dir=new File(getSavePath());
System.out.println(dir.getAbsolutePath());
//判斷是否存在路徑
if(!dir.exists()){
dir.mkdirs();
}
//當(dāng)前上傳的文件
File file=getImg_file();
//獲得后綴
String ext =FileUtil.getExtensionName(getImg_fileFileName());
String newFileName = UUID.randomUUID()+ext;
FileOutputStream fos=new FileOutputStream(getSavePath()+"http://"+newFileName);
FileInputStream fis=new FileInputStream(getImg_file());
byte []buffers=new byte[1024];
int len=0;
while((len=fis.read(buffers))!=-1){
fos.write(buffers,0,len);
}
fos.close();
fis.close();
// String uploadFileName = getImg_fileFileName();
dataMap.put("filename",newFileName);
return SUCCESS;
}
<!-- 文件上傳相關(guān)的 --> <action name="fileUploadAction" class="fileUploadAction"> <param name="savePath">E:/Tomcat6.0/webapps/uploads</param> <result type="json"> <param name="root">dataMap</param> </result> </action>
配置完以上的基本就OK了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例
- Struts2中圖片以base64方式上傳至數(shù)據(jù)庫(kù)
- Struts2+jquery.form.js實(shí)現(xiàn)圖片與文件上傳的方法
- Java以struts2為例介紹如何實(shí)現(xiàn)圖片上傳
- JS+Struts2多文件上傳實(shí)例詳解
- struts2實(shí)現(xiàn)多文件上傳
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼
- Java框架Struts2實(shí)現(xiàn)圖片上傳功能
相關(guān)文章
原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼
這篇文章主要介紹了原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Spring Security基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)認(rèn)證過程解析
這篇文章主要介紹了Spring Security基于數(shù)據(jù)庫(kù)實(shí)現(xiàn)認(rèn)證過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java中BeanUtils.copyProperties的11個(gè)坑總結(jié)
我們?nèi)粘i_發(fā)中,經(jīng)常涉及到DO、DTO、VO對(duì)象屬性拷貝賦值,很容易想到org.springframework.beans.BeanUtils的copyProperties,它會(huì)自動(dòng)通過反射機(jī)制獲取源對(duì)象和目標(biāo)對(duì)象的屬性,pyProperties,會(huì)有好幾個(gè)坑呢,本文將給大家總結(jié)一下遇到的坑,需要的朋友可以參考下2023-05-05
關(guān)于Idea中的.properties文件顯示問題
這篇文章主要介紹了關(guān)于Idea中的.properties文件顯示問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Springboot任務(wù)之異步任務(wù)的使用詳解
今天學(xué)習(xí)了一個(gè)新技能SpringBoot實(shí)現(xiàn)異步任務(wù),所以特地整理了本篇文章,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06

