SpringMVC札集(08)——文件上传
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
在本篇博客中将介绍利用SpringMVC实现文件上传
准备jar包
除了之前SpringMVC开发所必备的jar包外,还需要额外准备两个jar包用于文件上传,如下图所示:
配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置自动扫描 -->
<context:component-scan base-package="cn.com"></context:component-scan>
<!-- 配置注解开发所需的处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解开发所需的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- 开启文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>1048576000</value>
</property>
<property name="maxInMemorySize">
<value>1024</value>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 开启SpingMVC的图片上传,请参见代码第35-43行
- 配置bean的id为multipartResolver
- 配置上传的最大大小等属性
编写jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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>测试SpringMVC的文件上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/testUpload/uploadFile.do" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload"> <input type="submit" value="upload" />
</form>
</body>
</html>
- 设置表单上传方式为post
- 设置enctype的值为multipart/form-data
- 利用type为file类型的input上传文件
实现Controller
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月31日 上午11:38:26
* @info 描述信息:SpringMVC上传文件
*/
package cn.com.controller;
import java.io.File;
import java.util.Iterator;
import javax.servlet.ServletContext;
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.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
@RequestMapping("/testUpload")
public class SpringMVCUpload {
@RequestMapping("uploadFile")
public String uploadFile(HttpServletRequest request)throws Exception {
File uploadedFolderFile=new File("E:/upload");
if(!uploadedFolderFile.exists()){
uploadedFolderFile.mkdirs();
}
ServletContext servletContext=request.getSession().getServletContext();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(servletContext);
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile multipartFile = multiRequest.getFile(iterator.next());
if (multipartFile != null) {
String parentPath=uploadedFolderFile.getAbsolutePath()+File.separator;
String originalFilename = multipartFile.getOriginalFilename();
String path = parentPath + originalFilename;
File file=new File(path);
multipartFile.transferTo(file);
}
}
}
return "/test";
}
}
部署测试
选择图片后,点击upload上传。
嗯哼,现在来瞅瞅传到服务端的图片
OK!
SpringMVC札集(08)——文件上传的更多相关文章
- SSM框架之SpringMVC(5)文件上传
SpringMVC(5)文件上传 1.实现文件上传的前期准备 1.1.文件上传的必要前提 A form 表单的 enctype 取值必须是: multipart/form-data(默认值是:appl ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- SpringMVC注解方式与文件上传
目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...
- SpringMVC 通过commons-fileupload实现文件上传
目录 配置 web.xml SpringMVC配置文件 applicationContext.xml 文件上传 Controller 上传实现一 上传实现二 测试 依赖 配置 web.xml < ...
- springmvc学习笔记--支持文件上传和阿里云OSS API简介
前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...
- SpringMVC 使用MultipartFile实现文件上传(转)
http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...
- SpringMVC源码分析--文件上传
SpringMVC提供了文件上传的功能,接下来我们就简单了解一下SpringMVC文件上传的开发及大致过程. 首先需要在springMVC的配置文件中配置文件上传解析器 <bean id=&qu ...
- SSM-SpringMVC-32:SpringMVC中灌顶传授文件上传
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 我将用自认为最简单的语言,描述Springmvc的文件上传,来将老夫毕生功力灌顶传授给你 首先文件上传,又简至 ...
- SpringMVC之单/多文件上传
1.准备jar包(图标所指必备包,其他按情况导入) 2.项目结构 3.SingleController.java(控制器代码单文件和多文件) package com.wt.uplaod; import ...
随机推荐
- 设置 vadio 和checkbox是否选中
1.js方案 <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>te ...
- 彻底搞懂hashCode与equals的作用与区别及应当注意的细节
以前写程序一直没有注意hashCode的作用,一般都是覆盖了equals,缺没有覆盖hashCode,现在发现这是埋下了很多潜在的Bug!今天就来说一说hashCode和equals的作用. 先来试想 ...
- Hive架构
Hive组织数据包含四种层次:DataBase --> Table --> Partition --> Bucket,对应在HDFS上都是文件夹形式. 数据库和数据仓库的区别: 1) ...
- 从零开始玩转JMX(二)——Condition
Notification 一个MBean提供的管理接口允许代理对其管理资源进行控制和配置.然而,对管理复杂的分布式系统来说,这些接口知识提供了一部分功能.通常,管理应用程序需要对状态变化或者当特别情况 ...
- Nodejs Q promise设计思路
Nodejs Q promise库 前言 Q库为nodejs提供了一个基于promise的编程方式,从此避免了一层又一层的callback调用.不过Q的灵活性也给我造成了很大困扰,我可以用promis ...
- IPFS星际节点网站 IPNS域名解析教程
IPNS星际文件系统IPFS提供的域名命名空间,相当于经典HTTP协议中的DNS.只不过是,IPNS是将内容寻址的哈希值(HASH值)转换为域名,而DNS是将IP地址转换为域名. 前段时间,IPFS协 ...
- [WCF安全3]使用wsHttpBinding构建基于SSL与UserName授权的WCF应用程序
上一篇文章中介绍了如何使用wsHttpBinding构建UserName授权的WCF应用程序,本文将为您介绍如何使用wsHttpBinding构建基于SSL的UserName安全授权的WCF应用程序. ...
- codeforces208E Blood Cousins
题目链接:codeforces208E 正解:$dsu$ $on$ $tree$ 解题报告: 又是一波$dsu$ $on$ $tree$咯… $p$级$cousin$其实就是对于$x$的$p$级祖先统 ...
- js 捕获型事件
true 为捕获型事件 false 为冒泡型事件
- jsplumb 的初次使用
最近的项目要能创建流程, 流程配置什么的就找了 jsplumb 来做流程的显示配置.经过两天的研究成果已经很明显了 参考了以下一些大神们的博客: jsplumb 中文教程 连线绘图工具库介绍 附简单在 ...