springmvc fileupload
1.pom文件中fileupload的dependencyyinr
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2.springmvc配置文件
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传的最大限制 -->
<property name="maxUploadSize" value="209715200" />
<!-- 默认编码 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- 上传文件的解析 -->
<property name="resolveLazily" value="true" />
</bean>
3.前台jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<html>
<head> <script type="text/javascript" src="<%=basePath%>/js/jquery.min.js"></script>
<style type="text/css">
#pic{
width:200px;
height:200px;
/* border-radius:50% ; */
margin:20px auto;
cursor: pointer;
}
</style> </head>
<c:import url="main.jsp"></c:import>
<body>
<input id="serverPath" type="hidden" value="<%=basePath %>"/>
<form enctype="multipart/form-data" id="uploadForm">
<div>省:<input type="text" id="province"></div>
<div>市:<input type="text" id="city"></div>
<img id="pic" src="<%=basePath%>/images/upload_img.png" >
<input id="upload_file" name="upload_file" accept="image/*" type="file" style="display: none"/>
<input type="button" id="uploadPicButton" value="上传" onclick="uploadPic()">
</form>
</body>
</html>
4.js代码
var serverPath;
$(function() {
serverPath = $("#serverPath").val();
$("#pic").click(function () {
$("#upload_file").click(); //隐藏了input:file样式后,点击头像就可以本地上传
$("#upload_file").on("change",function(){
var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径,该路径不是图片在本地的路径
if (objUrl) {
$("#pic").attr("src", objUrl) ; //将图片路径存入src中,显示出图片
}
});
});
});
//建立一個可存取到該file的url
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
function uploadPic(){
var pic = $('#upload_file')[0].files[0];
var fd = new FormData();
fd.append('file', pic);
fd.append('province', $('#province').val());
fd.append('city', $('#city').val());
$.ajax({
url:serverPath + "/bmdList/test",
type:"post",
// Form数据
data: fd,
cache: false,
contentType: false,
processData: false,
success:function(data){
alert(data);
}
});
}
5.后台代码
@RequestMapping(value = "/test",method = RequestMethod.POST)
@ResponseBody
public String test(PhoneCardEntity entity) throws IOException{
System.out.println("省:"+entity.getProvince());
System.out.println("市:"+entity.getCity());
System.out.println(entity.getFile().getOriginalFilename());
byte[] bytes = entity.getFile().getBytes();
return "success";
}
6.entity代码
private String province; // 省
private String city; // 市
private MultipartFile file;
----------------------------------------------------------------------------------华丽的分割线----------------------------------------------------------------------------------------
执行效果
1/ 页面初始化

2.选择图片及输入内容

3.点击上传后台输出log

springmvc fileupload的更多相关文章
- 3.2.3 SpringMVC注解式开发
SpringMVC注解式开发 1. 搭建环境 (1) 后端控制器无需实现接口 , 添加相应注解 Controller类添加注解 @Controller //该注解表将当前类交给spring容器管理 @ ...
- SpringMVC的文件上传与下载
1. 单文件上传 配置jsp页面 <%@ page contentType="text/html;charset=UTF-8" language="java&quo ...
- spring之文件上传
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- dwz+jquery+fileupload+springmvc实现文件上传 及图片预览
1 前台jsp:文件的上传利用了iframe实现局部刷新功能.使用了apache的fileupload组件,用到的jar: commons-fileupload.jar,commons-io.jarD ...
- SpringMVC使用FileUpload上传文件
进口FileUpload和common-io的Jar包 注意:1.Struts2其它方法需要使用的:struts2过滤,将改变reqeust类型,由HttpServletRequest成为MultiP ...
- java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory报错springmvc
转自:https://blog.csdn.net/qq_41879385/article/details/82892555 下面是错误信息:java.lang.ClassNotFoundExcepti ...
- springMVC学习笔记--知识点总结1
以下是学习springmvc框架时的笔记整理: 结果跳转方式 1.设置ModelAndView,根据view的名称,和视图渲染器跳转到指定的页面. 比如jsp的视图渲染器是如下配置的: <!-- ...
- springmvc的图片上传与导出显示
1.前端文件上传需要在form表单内添加enctype="multipart/form-data" ,以二进制传递: 2.web.xml文件内配置 <servlet-mapp ...
- 基于Spring+SpringMVC+Mybatis的Web系统搭建
系统搭建的配置大同小异,本文在前人的基础上做了些许的改动,重写数据库,增加依据权限的动态菜单的实现,也增加了后台返回json格式数据的配置,详细参见完整源码. 主要的后端架构:Spring+Sprin ...
随机推荐
- django drf 级联数据和RetrieveModelMixin
1.定义View from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...
- cesium编程入门(六)添加 3D Tiles,并调整位置,贴地
添加 3D Tiles,并调整位置 3D Tiles 是什么 3DTiles数据集是cesium小组AnalyticlGraphics与2016年3月定义的一种数据集,3DTiles数据集以分块.分级 ...
- 「ZJOI 2010」 排列计数
题目链接 戳我 \(Solution\) 其实我们可以发现这题等价于让你求: 用\(1\)~\(n\)的数组成一个完全二叉树使之满足小根堆性质的方案数 于是我们可以考虑\(dp\) 假设我们现在在\( ...
- 百度地图API鼠标获取坐标
var map = new BMap.Map('map'); var poi = new BMap.Point(112.53, 37.87); map.enableScrollWheelZoom(); ...
- lambda 、 map 、filter 、reduce 及 reversed 常用函数
lambda 匿名函数 什么是lambda? lambda 操作符(或 lambda 函数)通常用来创建小巧的,一次性的匿名函数对象.它的基本语法如下: lambda arguments : expr ...
- “全栈2019”Java第八十九章:接口中能定义内部类吗?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 如何检查 IP是否冲突了
[root@TEST_192_168_1_252 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 44:A8:42:00:1A:B5 ...
- CentOS7打开关闭防火墙与端口
http://www.javahelp.com.cn/h-nd-747.html#_np=153_1707
- time 模块学习
前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...
- Oracle递归查询(start with)
写代码时碰到要弄清楚Oracle的role之间的传递关系,就是有role A的话,可以通过grant A to B,把A赋予给B,又通过grant B to C .那我想知道所有role中,有哪些ro ...