springMVC上传图片,使用jquery预览
2016-09-16 12:42
295 查看
首先在html加入代码:其中div用于显示上传后的图片预览
js代码如下:需要先引入jquery.form.js插件<script type="text/javascript" src="js/jquery-form.js"></script>使用ajaxSubmit
服务器端代码:
<form id="form" enctype="multipart/form-data"> upload image: <input type="file" id="image_input" name="file" /> <a href="#" onclick="upload()">upload</a> </form> <div id="imgDiv"></div>
js代码如下:需要先引入jquery.form.js插件<script type="text/javascript" src="js/jquery-form.js"></script>使用ajaxSubmit
function upload() { var imagePath = $("#image_input").val(); if (imagePath == "") { alert("please upload image file"); return false; } var strExtension = imagePath.substr(imagePath.lastIndexOf('.') + 1); if (strExtension != 'jpg' && strExtension != 'gif' && strExtension != 'png' && strExtension != 'bmp') { alert("please upload file that is a image"); return false; } $("#form").ajaxSubmit({ type : 'POST', url : 'upload/image.do', success : function(data) { alert("上传成功"); $("#imgDiv").empty(); $("#imgDiv").html('<img src="'+data+'"/>'); $("#imgDiv").show(); }, error : function() { alert("上传失败,请检查网络后重试"); } }); }
服务器端代码:
package com.shop.controller; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping("/upload/") public class ImageUploadController { /** * upload image and return the image url * * @return * @throws IOException * @throws IllegalStateException */ @RequestMapping("image") @ResponseBody public String uploadImage(HttpServletRequest request, HttpServletResponse response, HttpSession session, @RequestParam(value = "file", required = true) MultipartFile file) throws IllegalStateException, IOException { String path = session.getServletContext().getRealPath([b]"/upload"[/b]); System.out.println("real path: " + path); String fileName = file.getOriginalFilename(); System.out.println("file name: " + fileName); File targetFile = new File(path, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); } file.transferTo(targetFile); String fileUrl = request.getContextPath() + "/upload/" + fileName; return fileUrl; } }
相关文章推荐
- springmvc下使用formdata异步ajax上传图片
- springmvc使用ssi-uploader批量上传图片及后台接受
- SpringMvc异步图片上传回显(使用jersey上传到另一台服务器)
- SpringMVC上传图片总结(1)-Web Uploader--常规方法进行图片上传,使用了MultipartFile、MultipartHttpServletRequest
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
- 异步上传图片 springmvc的MultipartFile和jersey的使用
- 使用WebUploader跨域上传图片,用SpringMVC实现服务器端
- 微信小程序上传图片到服务器(java后台以及使用springmvc)
- 结合springmvc,使用ajax上传base64图片数据
- springmvc 中使用Jersey上传图片
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
- 使用SpringMVC配合jQuery文件上传插件ssi-uploader实现多图片可预览上传
- SpringMVC上传图片总结(2)--- 使用百度webuploader上传组件进行上传图片
- 使用bootstrap图片上传插件(fileInput)springmvc实现图片上传全流程
- 在JSP中使用ckeditor以及使用SpringMVC实现图片上传
- springmvc使用ssi-uploader批量上传图片及后台接受
- 结合springmvc,使用ajax上传base64图片数据
- JAVA 使用springMVC 上传多张图片或文件,并对图片进行按比例缩放处理
- 图片使用base64图片数据格式上传(后台使用springMVC)
- UMeditor与SpringMVC的整合和上传图片使用