spirngMVC +SWFUpload上传文件

1.Controller文件如下:

public ModelAndView upload(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		try{
			String uploadDir = getServletContext().getRealPath("/upload");
			File dirPath = new File(uploadDir);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
			
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filedata");//这里是表单的名字,在swfupload.js中this.ensureDefault("file_post_name", "filedata");
			
			InputStream stream = file.getInputStream();
			String fileName = file.getOriginalFilename();
			fileName = new String(fileName.getBytes(),"utf-8");
			String fileNameFull = uploadDir + Constants.FILE_SEP + fileName;
			OutputStream bos = new FileOutputStream(fileNameFull);
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}

			bos.close();
			// close the stream
			stream.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return new ModelAndView("checkReport/checkReportSearchTable");//这里要制定返回页,如果返回Null,上传没问题,但是上传完毕后,页面会弹出404错误
	}


2.在jsp页面引入两个必要的swfupload的JS文件:

<script type="text/javascript" src="scripts/js/swfupload.js"></script> 
<script type="text/javascript" src="scripts/js/handlers.js"></script> 

javaScript代码:

var swfu;
		window.onload = function(){
			swfu = new SWFUpload({
				// Backend Settings
				upload_url: "unitProductValueController.html?method=upload",	//上传或解析处理类的URL
                
				// File Upload Settings
				file_size_limit : 0, // 限制上传文件的大小, 0表示不受限制
				file_types : "*.xls;*.txt;*.pdf;*.doc;*.xlsx;*.docx",           // 上传文件类型,多个文件用分号隔开
				file_types_description : "", // 上传文件类型描述
				file_upload_limit : "0",    // 限制上传文件选择的个数, "0"表示不受限制
		
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_progress_handler : uploadProgress,         //上传过程中调用的方法
				upload_error_handler : uploadError,               //上传错误调用的方法
				upload_success_handler : uploadSuccess,           
				upload_complete_handler : uploadComplete,         //上传完成调用的方法
				
				//post_params : {
				//	'state':'1234567'
				//},
				//post_params :{};

				// Button settings
			    button_image_url : "",	// Relative to the SWF file
			    button_placeholder_id : "spanButtonPlaceholder",//html标签ID,如DIV或SPAN
			    button_width: 100,
			    button_height: 22,
			    button_text : '<u>添加附件</u>',//生成的按钮内容
		    	button_text_style : '.button {font-size: 14pt;cursor:pointer;text-align:right} .buttonSmall { font-size: 10pt;cursor:pointer;text-align:right }',
			    button_text_top_padding: 1,
			    button_text_left_padding: 45,

				// Flash Settings
				flash_url : "<psmis:webRoot/>scripts/js/swfupload.swf",	// Relative to this file
				//上传或解析过程中,显示进度信息
				custom_settings : {
					upload_target : "divFileProgressContainer"
				},
				// Debug Settings
				debug: false
			});
		}

3.html表单:
<form action="unitProductValueController.html?method=upload" id="formMessage"  method="post" enctype="multipart/form-data">
 <table width="100%" id="messageTable" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse" bordercolor="#1372A0">
 <tr> 
<td colspan="2" style="height:30px;"> 
<span id="enterpriseName"></span> 
</td> 
</tr> 
<tr> 
<td style="text-align:right">邮件类型:</td> 
<td style="height:30px;width: 80%;"> 
<span style="float:left;" id="messageTypeSpan"> 
<input type="radio" name="messageType" value="1" checked="checked" id="messageType1" style="border:0px"><label for="messageType1">催报邮件</label>
 <input type="radio" name="messageType" value="2" id="messageType2" style="border:0px"><label for="messageType2">重报邮件</label>
 </span> 
</td>    
</tr> 
<tr> 
<td   style="text-align:right">主题:</td> 
<td  style="height:30px;text-align:center"> 
<input type="text" name="subject" name="subject" style="width:98%"> 
</td> 
</tr> 
<tr> 
<td  style="text-align:right"> 
<div id="swfu_container" style="margin: 0px 5px;"> 
<div> 
<span id="spanButtonPlaceholder"></span> 
</div> 
</div> 
</td> 
<td  style="height:30px;"> 
<div id="divFileProgressContainer" style="height: 30px;float:left;margin-left:4px;"></div>
 </td> 
</tr> 
<tr> 
<td style="text-align:right;vertical-align:top;padding-top:3px;">邮件内容:</td> 
<td valign="top" > 
<textarea name="emailContent"   id="emailContent" style="height:150px;width:350px;border:0px solid  #000000;float:left "></textarea>
 <select multiple="multiple" id="consignee" name="consignee" size="9" style="vertical-align:top;float:right;margin-right:5px;width:120px;">
 <option>模板1</option> 
<option>模板2</option> 
<option>模板3</option> 
</select> 
</td> 
</tr> 
            <tr> 
<td  align="right" colspan="2"> 
<input type="button" name="ibtnSearch" value="发送" id="ibtnSearch" class="com_btn2"  onclick="saveMessage()"/>
 <input type="button" name="iBtnSaveQuery" value="重置" 
id="iBtnSaveQuery" class="com_btn2" onclick="document.getElementById('textarea').value='';" />
 </td> 
</tr> 
</table> 
</form> 

你可能感兴趣的:(JavaScript,html,jsp,Flash)