springmvc的图片上传与导出显示
1.前端文件上传需要在form表单内添加enctype="multipart/form-data" ,以二进制传递:
2.web.xml文件内配置
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.springmvc.xml文件配置
<!-- 文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="900000000000"/>
</bean>
4.Controlle里面接受MultipartFile,在tomcat内创建虚拟盘指向盘符比如"E:\\picture\\"
将图片名字存储到数据库内
以下为为实现代码:
jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <meta name="keywords" content="响应式后台"> <meta name="description" content="使用了Html5+CSS3等现代技术"> <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet"> <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet"> <link href="css/plugins/blueimp/css/blueimp-gallery.min.css" rel="stylesheet"> <link href="css/animate.min.css" rel="stylesheet"> <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet"> <link href="css/style.css" rel="stylesheet" type="text/css" /> <style> .lightBoxGallery img { margin: 5px; height : 150px; } </style> <script type="text/javascript"> window.onload = function() { var w = 250;//设置最大宽度,也可根据img的外部容器 而动态获得,比如:$("#demo").width(); $("img").each(function() {//如果有很多图片,使用each()遍历 var img_w = $(this).width();//图片宽度 var img_h = $(this).height();//图片高度 if (img_w > w) {//如果图片宽度超出指定最大宽度 var height = (w * img_h) / img_w; //高度等比缩放 $(this).css( { "width" : w });//设置缩放后的宽度和高度 } }); } $(document).ready(function() { }) </script> </head> <body class="gray-bg"> <div class="wrapper wrapper-content"> <div class="row"> <div class="col-lg-12"> <div class="ibox float-e-margins"> <div class="ibox-content"> <div> <sf:form action="fileUpload.do" method="post" enctype="multipart/form-data" > <table> <tr> <td>上传风险实景图:</td> <td><input type="file" name="pic"/></td> <td><input type="submit" value="上传"></td> </tr> </table> </sf:form> </div> <div class="lightBoxGallery"> <c:if test="${not empty pictureList}"> <c:forEach items="${pictureList}" var="p"> <a href="/pp/${p.name}" class="big" title="风险图" data-gallery=""><img class="img" src="/pp/${p.name}"></a> </c:forEach> </c:if> <div id="blueimp-gallery" class="blueimp-gallery"> <div class="slides"></div> <h3 class="title"></h3> <a class="prev"><</a> <a class="next">></a> <a class="close">×</a> <a class="play-pause"></a> <ol class="indicator"></ol> </div> </div> </div> </div> </div> </div> </div> <script src="js/jquery.min.js?v=2.1.4"></script> <script src="js/bootstrap.min.js?v=3.3.6"></script> <script src="js/content.min.js?v=1.0.0"></script> <script src="js/plugins/blueimp/jquery.blueimp-gallery.min.js"></script> <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script> </body> </html>
Controller类里面代码:
package com.is.picture.control; import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import com.is.picture.bean.Picture; import com.is.picture.service.pictureService; @Controller public class pictureControl { @Resource private pictureService pictureservice; @RequestMapping(value="picture") public ModelAndView pictureList(HttpServletRequest request) { ModelAndView mav = new ModelAndView(); try { List<Picture> pictureList = pictureservice.findAll(); System.out.println(pictureList.size()); mav.addObject("pictureList", pictureList); mav.setViewName("picture/NewFile"); System.out.println("明明"); } catch (Exception e) { e.printStackTrace(); } return mav; } @RequestMapping(value="fileUpload") public ModelAndView picturesc(MultipartFile pic) { Picture pt = new Picture(); ModelAndView modelAndView = new ModelAndView(); try { String originalName = pic.getOriginalFilename(); if(pic != null && originalName != null && originalName.length()>0){ //1、保存图片的物理路径 String store = "E:\\picture\\"; //2、新的图片名称 String picNewName = UUID.randomUUID()+originalName.substring(originalName.lastIndexOf(".")); //3、新图片产生 File file = new File(store+picNewName); //4、将内存中的数据写入磁盘 pic.transferTo(file); pt.setName(picNewName); } pictureservice.add(pt); modelAndView.setViewName("redirect:pictureSave.do"); System.out.println("你说呢"); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return modelAndView; } }