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. boosting、adaboost

    1.boosting Boosting方法是一种用来提高弱分类算法准确度的方法,这种方法通过构造一个预测函数系列,然后以一定的方式将他们组合成一个预测函数.他是一种框架算法,主要是通过对样本集的操作获 ...

  2. Windows 7上执行Cake 报错原因是Powershell 版本问题

    在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...

  3. Js 原型和原型链

    Js中通过原型和原型链实现了继承 Js对象属性的访问,首先会查找自身是否拥有这个属性 如果查到,则返回属性值,如果找不到,就会遍历原型链,一层一层的查找,如果找到就会返回属性值 直到遍历完Object ...

  4. .NET Core的日志[1]:采用统一的模式记录日志

    记录各种级别的日志是所有应用不可或缺的功能.关于日志记录的实现,我们有太多第三方框架可供选择,比如Log4Net.NLog.Loggr和Serilog 等,当然我们还可以选择微软原生的诊断框架(相关A ...

  5. redis集成到Springmvc中及使用实例

    redis是现在主流的缓存工具了,因为使用简单.高效且对服务器要求较小,用于大数据量下的缓存 spring也提供了对redis的支持: org.springframework.data.redis.c ...

  6. spring源码分析之context

    重点类: 1.ApplicationContext是核心接口,它为一个应用提供了环境配置.当应用在运行时ApplicationContext是只读的,但你可以在该接口的实现中来支持reload功能. ...

  7. # PHP - 使用PHPMailer发邮件

    PHPMailer支持多种邮件发送方式,使用起来非常简单 1.下载PHPMailer https://github.com/PHPMailer/PHPMailer,下载完成加压后, 把下边的两个文件复 ...

  8. error C4430:missing type specifier 解决错误

    错误    3    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...

  9. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  10. 解决maven下载jar慢的问题(如何更换Maven下载源)

    修改 配置文件 maven 安装 路径 F:\apache-maven-3.3.9\conf 修改 settings.xml 在 <mirrors> <!-- mirror | Sp ...