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;
}
}

springmvc的图片上传与导出显示的更多相关文章

  1. springmvc之图片上传

    1.接收到的是图片的流时 //上传头像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod ...

  2. SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传

    关于springMVC来实现图片上传的功能 话不多说,直接上码 1.applicationContext.xml <!-- 配置文件上传 --> <!--200*1024*1024即 ...

  3. laravel7 图片上传及视图显示

    1:修改框架config下的文件filesystems.php中的配置: 原文件 <?php return [ /* |------------------------------------- ...

  4. 微擎 plugin 时间插件 图片上传插件不显示 报错 影响下面执行

    可能是版本更新导致的,之前可能不需要 load()->func('tpl');这个方法 现在加上 load()->func('tpl');应该就可以了

  5. springmvc图片上传、json

    springmvc的图片上传 1.导入相应的pom依赖 <dependency> <groupId>commons-fileupload</groupId> < ...

  6. input file 图片上传

    使用第三方:jquery.ajaxfileupload.jsinput中的name根据后端来定 <form method="post" enctype="multi ...

  7. DWZ集成的xhEditor编辑器浏览本地图片上传的设置

    有关xhEditor的文件上传配置官方文档链接:http://i.hdu.edu.cn/dcp/dcp/comm/xheditor/demos/demo08.html 一.xhEditor图片上传的配 ...

  8. Silverlight中的拖拽实现的图片上传

    原文 http://blog.csdn.net/dujingjing1230/article/details/5443003 在Silverlight中因为可以直接从系统的文件夹里面拖出来一个文件直接 ...

  9. springmvc上传图片并显示图片--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

随机推荐

  1. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  2. 一道返回num值的小题目

    题目描述: 实现fizzBuzz函数,参数num与返回值的关系如下: .如果num能同时被3和5整除,返回字符串fizzbuzz .如果num能被3整除,返回字符串fizz .如果num能被5整除,返 ...

  3. redis 学习笔记(1)

    redis持久化 snapshot数据快照(rdb) 这是一种定时将redis内存中的数据写入磁盘文件的一种方案,这样保留这一时刻redis中的数据镜像,用于意外回滚.redis的snapshot的格 ...

  4. pt-online-schema-change中update触发器的bug

    pt-online-schema-change在对表进行表结构变更时,会创建三个触发器. 如下文测试案例中的t2表,表结构如下: mysql> show create table t2\G . ...

  5. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  6. spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析

    1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...

  7. 香蕉云APP,2016下半年开发日记

    2016-6-17  数据库设计不应该过多依赖范式,适度的冗余可以加快搜索速度,在服务器的配置还可以的情况下,可以采用冗余来解决查找慢的问题.还一个是要选择好数据库引擎,例如 InnoDB 和 myi ...

  8. CSS样式重置(转)

    body,h1,h2,h3,h4,h5,h6,dl,dt,dd,ul,ol,li,th,td,p,blockquote,pre,form,fieldset,legend,input,button,te ...

  9. 模仿Linux内核kfifo实现的循环缓存

    想实现个循环缓冲区(Circular Buffer),搜了些资料多数是基于循环队列的实现方式.使用一个变量存放缓冲区中的数据长度或者空出来一个空间来判断缓冲区是否满了.偶然间看到分析Linux内核的循 ...

  10. python 数据类型---文件二

    1.打印进度条 import sys,time for i in range(20): sys.stdout.write("#") sys.stdout.flush() #不等缓冲 ...