六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 145|回复: 0

spirngMVC +SWFUpload上传文件

[复制链接]

升级  36%

4

主题

4

主题

4

主题

童生

Rank: 1

积分
18
 楼主| 发表于 2013-2-7 19:57:49 | 显示全部楼层 |阅读模式
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 streamstream.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 Settingsupload_url: "unitProductValueController.html?method=upload",//上传或解析处理类的URL                // File Upload Settingsfile_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 Settingsflash_url : "<psmis:webRoot/>scripts/js/swfupload.swf",// Relative to this file//上传或解析过程中,显示进度信息custom_settings : {upload_target : "divFileProgressContainer"},// Debug Settingsdebug: 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"  /> <input type="button" name="iBtnSaveQuery" value="重置" id="iBtnSaveQuery" class="com_btn2"  /> </td> </tr> </table> </form>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表