Struts1+jquery+jform插件实现文件同步和异步上传

JS代码:无限级添加附件 jquery.js jquery-form.js

<script type="text/javascript" language="javascript">

function AddAttachment() {
var objTable = $("#id_attachmentpanel");
var intCount = $("#id_attachmentpanel tr").children().size() + 1;
objTable.append("<tr><td>" + intCount + " <input type='file' name='file"
+ intCount + "' onchange='FileExtChecking(this,1)' /></td><a href='javascript:void(0);' onclick='DisposeTr(this)'>[删除]</a></td></tr>");
}

function DisposeTr(arg_obj_item) {
var objTr = $(arg_obj_item).parent();
objTr.remove();
}

function egdTB_Upload_ClickedEx(){
alert("进入异步方法.....");
var saveUrl = contextPath + '/returnMessageAction.do?action=saveByAjax';
$(document).ready(function(){
var options = {
url : saveUrl,
type : "POST",
dataType : "json",
success : function(json) {
alert("显示成功提示!");
alert("提示信息:"+json.msg);
alert("显示是否成功:"+json.result);
}
};
$("#thisForm").ajaxSubmit(options);
return false;
});
}

</script>

HTML代码:

<html:form action="returnMessageAction.do?action=saveByAjax"
styleId="thisForm" method="post" enctype="multipart/form-data">
<!-- 大标题区 -->
<table width="100%" border="0" cellpadding="0" cellspacing="0"
class="egd-form-title-warp egd-form-pageWidth"
style="height: 28px">
<tr>
<td class="egd-page-title-text">
信息反馈
</td>
</tr>
</table>
<div class="egd-form-pageWidth"
style="text-align: right; margin-top: 10px;"></div>
<!-- 表单区 -->
<div class="egd-form-pageWidth egd-page-form-wrap">
<!-- 表单边颜色和背景颜色 -->
<div class="egd-page-form-tableBorder">
<table width="100%" border="0" cellpadding="1" cellspacing="2">
<tr class="egd-form-emptyTR">
<td width="80"></td>
<td width="270"></td>
<td width="80"></td>
<td></td>
</tr>

<tr>
<td class="egd-form-label">
主题:
</td>
<td class="egd-property">
<html:text property="topic" styleClass="egd-form-btField"
maxlength="32" />
</td>

</tr>
<tr>
<td class="egd-form-label">
内容:
</td>
<td class="egd-property">
<html:text property="content" styleClass="egd-form-btField"
maxlength="32" />
</td>
</tr>
</table>
</div>
</div>
<!-- 隐藏数据区 -->
<html:hidden property="id" styleId="id" />

<a href="javascript:void(0)" onclick="AddAttachment()">[点击添加附件]</a>
<table cellpadding="0" cellspacing="3" id="id_attachmentpanel">
</table>


</html:form>

Struts代码:

List formsFiles = new ArrayList();
// 得到所有的文件请求元素
Hashtable files = form.getMultipartRequestHandler().getFileElements();
if (files != null && files.size() > 0){
// 得到files的keys
Enumeration enums = files.keys();
String fileKey = null;
// 遍历枚举
while (enums.hasMoreElements()){
// 取得key
fileKey = (String) (enums.nextElement());
FormFile formFile = (FormFile)files.get(fileKey);
formsFiles.add(formFile);
}
UplaodAndDownUtil.uploadFile(formsFiles, request);
}

/**
* 信息反馈--附件上传 下载action 无用
* @author Administrator
*/
public class UplaodAndDownUtil extends EgrandAction {

public static void uploadFile(List files, HttpServletRequest request) {
if(null == files || files.size() ==0){
return;
}
// 得到当前网站的绝对路径
String projectPath = request.getSession().getServletContext().getRealPath("/")+"\\upload";
File uploadFile = new File(projectPath);
if (!uploadFile.exists() || uploadFile == null) {
uploadFile.mkdirs();
}
FileOutputStream fileOutput = null;
try {
for(int i=0;i<files.size();i++){
FormFile file = (FormFile) files.get(i);
if ("".equals(file.getFileName()) || null == file.getFileName()) {
return;
}
fileOutput = new FileOutputStream(uploadFile.getPath() + "\\" + file.getFileName());
fileOutput.write(file.getFileData());
}
fileOutput.flush();
fileOutput.close();
} catch (FileNotFoundException e1) {
System.out.println("BbsAction:找不到文件");
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("BbsAction:文件IO异常");
e1.printStackTrace();
}
}
}

你可能感兴趣的:(struts1)