SpringMVC+ajaxFileUpload 异步上传
来源:互联网 发布:python写搜索引擎 编辑:程序博客网 时间:2024/05/17 05:03
jsp页面
<form id="input_group" enctype="multipart/form-data" method="post" action="upload.do"><input type="file" id="motifImage_hidden" name="motifImage_hidden" class="form-control" accept="image/*" style="display:none;" onchange="javascript:selectImage_setVal(this);">'</form>
function selectImage_setVal(dom){$(dom).parent().find('>#motifImage').val($(dom).val());$.ajaxFileUpload({url: '<%=basePath%>admin/motif/uploadfile.shtml', type: 'post',secureuri: false, //一般设置为falsefileElementId: 'motifImage_hidden', // 上传文件的id、name属性名dataType: 'json', //返回值类型,一般设置为json、application/jsonsuccess: function(data){//下面的值按照个人需求更改$('#motifImage').attr('originImgName',data.filename);$('#motifImage').attr('Imgpath',data.path);$('#motifImage').attr('newImgName',data.newFilename);},error: function(data, status, e){ alert("呵呵");}});}
</pre><p><strong><span style="font-size:24px;">Controller层</span></strong></p><pre name="code" class="javascript">
//图片上传@RequestMapping(value = "uploadfile")public void upload(@RequestParam(value ="motifImage_hidden") MultipartFile myfile,HttpServletResponse response, HttpServletRequest request) throws IOException {JSONObject jsonObject = new JSONObject();String filename = myfile.getOriginalFilename();//图片上传位置//String logoRealPathDir = request.getSession().getServletContext().getRealPath(“/upload”); String path = "D:\\upload";//文件名更改为时间磋String newFilename = this.getName(filename);File targetFile = new File(path, newFilename); if(!targetFile.exists()){ targetFile.mkdirs(); } try { myfile.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } jsonObject.put("filename", filename); jsonObject.put("path", path+"\\"+newFilename); jsonObject.put("newFilename", newFilename);response.setContentType("textml;charset=UTF-8");response.setCharacterEncoding("utf-8");response.getWriter().println(jsonObject);}
/** * 依据原始文件名生成新文件名 * @return */private String getName(String fileName) {Random random = new Random();return "" + random.nextInt(10000)+ System.currentTimeMillis() + this.getFileExt(fileName);}/** * 获取文件扩展名 * * @return string */private String getFileExt(String fileName) {return fileName.substring(fileName.lastIndexOf("."));}
此文章只是个人纪录学习代码的过程,有些参考网上.
1 0
- SpringMVC+ajaxFileUpload 异步上传
- springmvc+ajaxfileupload异步上传
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- springmvc + ajaxfileupload 实现异步上传文件(图片)
- springmvc + ajaxfileupload 实现异步上传文件(图片)
- SpringMVC结合ajaxfileupload.js实现异步上传文件
- springmvc + ajaxfileupload 实现异步上传文件(图片)
- freemarker+springMVC+ajaxfileupload实现异步图片上传(单张)
- freemarker+springMVC+ajaxfileupload实现异步图片上传(多张)
- SpringMVC+ajaxFileUpload 兼容IE浏览器实现异步上传图片
- springMVC + ajaxfileupload异步上传图片预览,裁剪并保存图片
- ajaxFileUpload 异步上传文件
- ajaxfileupload异步上传文件
- ajaxfileupload异步上传文件
- ajaxFileupload异步上传图片
- SpringMVC+ajaxfileupload上传
- Caffe Model Zoo
- Spring AOP入门Demo
- The identity used to sign the executable is no longer valid.
- PHP设计模式总结
- QT中显示图像数据
- SpringMVC+ajaxFileUpload 异步上传
- 【网络安全】移动安全软件合集
- CSS中的绝对定位与相对定位 【转载】
- iOS中如何从应用中跳转到系统的设置界面中的内容
- Swift百万线程攻破单例(Singleton)模式
- 单文档标题的修改
- Qt自适应大小显示图片,添加菜单
- casperJs 获取结果值
- iOS7 NavigationController 手势问题 interactivePopGestureRecognizer