1. 使用input type=file 进行文件上传
点击按钮后,action中file得到对应与input 中的文件,此时File 就是你提交的文件了,然后将文件复制到服务器则完成上传,此方法一个input 只能上传一个文件
jsp端:<input type="file" name="file" id="file"/>
<s:form id="form" name="form" method="post" action="importTest" namespace="/test" enctype="multipart/form-data" theme="simple">
java端:private File file; 这个file 对应于 name中的file(同时get set 方法必须有)
2.使用swfupload 上传文件
jsp端:
需要的js文件:
<script type="text/javascript" src="<%=path %>/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="<%=path %>/js/swfupload/handlers.js"></script>
其中handlers.js 是具体的处理function ,如需不同的操作,可以修改此文件中的方法
window.οnlοad=function(){
swfu=new SWFUpload({
upload_url:<span style="background-color: rgb(255, 0, 0);"><span style="color:#000000;"> "<%=path%>/resource/uploadAccesay.action",(上传文件处理的action设置)</span>
</span> file_post_name : "<span style="background-color: rgb(255, 0, 0);">accessoryFile</span>",(<span style="background-color: rgb(255, 0, 0);">对应于action中的File <span style="background-color: rgb(255, 0, 0);">accessoryFile,文件名为accessoryFileFileName</span></span>)
file_upload_limit :<span style="background-color: rgb(255, 0, 0);"> "1",(上传个数限制 0为不限制)
</span> file_size_limit : <span style="background-color: rgb(255, 0, 0);">'0 MB', (上传文件大小限制(0为不限制))</span>
file_types : '*.*'<span style="color:#ff0000;">,(上传的文件类型控制)
</span> file_types_description : '所有文件',
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,//选择好文件后提交
file_queued_handler : fileQueued,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler :function(file, serverData){
try {
var progress = new FileProgress(file, this.customSettings.upload_target);
if(serverData==0){
addFileInfo(file.id,"文件上传失败");
Dialog.alert('<s:i18n name="MessageResources"><s:text name="L_UploadError"></s:text></s:i18n>');
}else if(serverData==1){
addFileInfo(file.id,"文件上传失败!文件太大");
Dialog.alert('<s:i18n name="MessageResources"><s:text name="L_UploadError"></s:text></s:i18n>');
}else if(serverData==2){
addFileInfo(file.id,"文件上传失败!文件类型不对");
Dialog.alert('<s:i18n name="MessageResources"><s:text name="L_UploadError"></s:text></s:i18n>');
}else if(serverData==12){
addFileInfo(file.id,"文件上传失败!文件太大且类型不对");
Dialog.alert('<s:i18n name="MessageResources"><s:text name="L_UploadError"></s:text></s:i18n>');
}else if(serverData==3){
addFileInfo(file.id,"文件上传失败");
Dialog.alert('<s:i18n name="MessageResources"><s:text name="L_UploadError"></s:text></s:i18n>');
}else {
addFileInfo(file.id,"<span style='color:green;'>上传成功</span>");
$("#addresource").submit();
}
} catch (ex) {
this.debug(ex);
}
},
upload_complete_handler : uploadComplete,
// Button Settings
button_image_url : "<%=path%>/js/swfupload/images/XPButtonUploadText_61x22.png",
button_placeholder_id : "spanButtonPlaceholder",
button_width: 100,
button_height: 25,
button_text : '<span class="button"></span>',
button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: 0,
button_text_left_padding: 18,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
//button_action:SWFUpload.BUTTON_ACTION.SELECT_FILE,//只能上传一个文件
// Flash Settings
flash_url : "<%=path%>/js/swfupload/swfupload.swf",
custom_settings : {
upload_target : "progress"
},
// Debug Settings
debug: false //是否显示调试窗口
});
点击按钮后,需要显示的调用swfu.startUpload(); 此方法一次只能上传一个文件 如需多个文件上传 需要在js中显示的调用此方法
action中使用同名的File 对象进行接收