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. Js new到底发生了什么

    在Js中,我们使用了new关键字来进行实例化 那么在这个new的过程中到底发生了什么? 关于构造函数的return 正常来讲构造函数中是不用写return语句的,因为它会默认返回新创建的对象. 但是, ...

  2. JavaScript的继承实现方式

    1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...

  3. 使用python抓取婚恋网用户数据并用决策树生成自己择偶观

    最近在看<机器学习实战>的时候萌生了一个想法,自己去网上爬一些数据按照书上的方法处理一下,不仅可以加深自己对书本的理解,顺便还可以在github拉拉人气.刚好在看决策树这一章,书里面的理论 ...

  4. C#多线程之线程池篇2

    在上一篇C#多线程之线程池篇1中,我们主要学习了如何在线程池中调用委托以及如何在线程池中执行异步操作,在这篇中,我们将学习线程池和并行度.实现取消选项的相关知识. 三.线程池和并行度 在这一小节中,我 ...

  5. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  6. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  7. const let,console.log('a',a)跟console.log('a'+a)的区别

    const 创建一个只读的常量 let块级作用域 const let重复赋值都会报错 console.log('a',a) a console.log('a'+a) a2 逗号的值会有空格:用加号的值 ...

  8. MySQL常见面试题

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  9. web.xml中welcome-file-list的作用

    今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...

  10. jsp富文本图片和数据上传

    好记性不如烂笔头,记录一下. 2016的最后一天,以一篇博客结尾迎接新的一年. 此处用的富文本编辑器是wangEditor,一款开源的轻量级的富文本编辑器,这里着重说一下里面的图片上传功能. 服务器端 ...