java代码
@RequestMapping("/upload") @ResponseBody public String upload(@RequestParam("file1")MultipartFile file,HttpServletRequest request,TdIndexSlideAd ad,Model model)throws Exception{ String path="E:/upload"; String filepath=path+"/"+file.getOriginalFilename();
InputStream fis=null; FileOutputStream fos=null;
try { fis=file.getInputStream(); fos=new FileOutputStream(new File(filepath)); byte[] bbs = new byte[1024]; int len = -1; while(-1 != (len=fis.read(bbs))){ fos.write(bbs,len); } TdIndexSlideAd ads=new TdIndexSlideAd(); ads.setAdName(ad.getAdName()); TSUser tsUser = ResourceUtil.getSessionUserName(); ads.setCreateId(tsUser.getId()); ads.setImagePath(filepath); ads.setIsValid(ad.getIsValid()); ads.setUrl(ad.getUrl()); tdIndexSlideAdService.save(ads);
} catch (Exception e) { e.printStackTrace(); }finally{ if(fos!=null){ fos.close(); } if(fis!=null){ fis.close(); } } } return "yes"; }
jsp 代码 需要引入ajaxfileupload.js插件
<script src="/plug-in/ajaxfileupload/ajaxfileupload.js" type="text/javascript"></script> <script type="text/javascript">
function uploadFile() { $.ajaxFileUpload({ url:"tdIndexSlideAdController/upload.do", data:{ "adName":$("#adName").val(), "imagePath":$("#imagePath").val(), "url":$("#url").val(), "isValid":$("#isValid").val() }, secureuri : false, fileElementId : 'file1', dataType : 'json', success: function(data,status){ var win = frameElement.api.opener; if(data=="yes"){ frameElement.api.close(); win.tip("上传成功"); }else{ frameElement.api.close(); win.tip("上传失败"); } win.reloadTable(); }, error : function(data,status,e)//服务器响应失败处理函数 { alert(e); } }); }
</script>
fileElementId 文件域的ID
secureuri //是否需要安全协议,一般设置为false <iframe id='frameFile' name='frameFile' style='display: none;'></iframe> <form target="frameFile"> <div class="form"> <label class="Validform_label"> 广告名称: </label> <input name="adName" id="adName" class="inputxt" > <span class="Validform_checktip">广告名称2~15位字符之间,且不为空</span> </div> <div class="form"> <label class="Validform_label">超链接(URL)</label> <input name="url" value="" class="inputxt" id="url" > <span class="Validform_checktip">角色范围2~8位字符之间,且不为空</span> </div> <div class="form"> <label class="Validform_label"> 是否有效: </label> <select name="isValid" id="isValid" > <option value="1">有效</option> <option value="2">无效</option> </select> </div> <div class="form"> <label class="Validform_label">选择图片</label> <input type="file" name="file1" value="" id="file1" /> </div> <div style="margin-left: 120px;margin-top: 20px"> <input type="button" value="确认上传" id="btn" > </div> </form>
如果 要在页面显示 需要加上下面的方法
/** * 显示图片 * @param request * @param response * @param filePath */ @RequestMapping(params="showPicture") public void showPicture(HttpServletRequest request,HttpServletResponse response,String filePath){ ServletOutputStream out = null; FileInputStream fis = null; try { File file=new File(filePath); fis=new FileInputStream(file); response.setContentType("image/*"); out=response.getOutputStream(); int i = fis.available();//文件大小 byte[] date = new byte[i]; fis.read(date); fis.close(); out.write(date); out.close(); } catch (Exception e) { e.printStackTrace(); } }
在jsp页面中加上
<imgsrc="tdIndexSlideAdController.do?showPicture&filePath=${slideAd.imagePath}"alt="" width="200" height="150"> (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|