自定义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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  16. http://www.springframework.org/schema/tx
  17. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  18. <!-- 配置自动扫描 -->
  19. <context:component-scan base-package="cn.com"></context:component-scan>
  20. <!-- 配置注解开发所需的处理器映射器-->
  21. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
  22. <!-- 配置注解开发所需的处理器适配器 -->
  23. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  24. <property name="messageConverters">
  25. <list>
  26. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  27. </list>
  28. </property>
  29. </bean>
  30. <!-- 开启文件上传 -->
  31. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  32. <property name="maxUploadSize">
  33. <value>1048576000</value>
  34. </property>
  35. <property name="maxInMemorySize">
  36. <value>1024</value>
  37. </property>
  38. </bean>
  39. <!-- 视图解析器 -->
  40. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  41. <property name="prefix" value="/WEB-INF/jsps/"></property>
  42. <property name="suffix" value=".jsp"></property>
  43. </bean>
  44. </beans>
  • 开启SpingMVC的图片上传,请参见代码第35-43行
  • 配置bean的id为multipartResolver
  • 配置上传的最大大小等属性

编写jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>测试SpringMVC的文件上传</title>
  7. </head>
  8. <body>
  9. <form action="${pageContext.request.contextPath }/testUpload/uploadFile.do" method="post" enctype="multipart/form-data">
  10. <input type="file" name="fileupload"> <input type="submit" value="upload" />
  11. </form>
  12. </body>
  13. </html>
  • 设置表单上传方式为post
  • 设置enctype的值为multipart/form-data
  • 利用type为file类型的input上传文件

实现Controller

  1. /**
  2. * @author 原创作者:谷哥的小弟
  3. * @blog 博客地址:http://blog.csdn.net/lfdfhl
  4. * @time 创建时间:2017年7月31日 上午11:38:26
  5. * @info 描述信息:SpringMVC上传文件
  6. */
  7. package cn.com.controller;
  8. import java.io.File;
  9. import java.util.Iterator;
  10. import javax.servlet.ServletContext;
  11. import javax.servlet.http.HttpServletRequest;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import org.springframework.web.multipart.MultipartHttpServletRequest;
  16. import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  17. @Controller
  18. @RequestMapping("/testUpload")
  19. public class SpringMVCUpload {
  20. @RequestMapping("uploadFile")
  21. public String uploadFile(HttpServletRequest request)throws Exception {
  22. File uploadedFolderFile=new File("E:/upload");
  23. if(!uploadedFolderFile.exists()){
  24. uploadedFolderFile.mkdirs();
  25. }
  26. ServletContext servletContext=request.getSession().getServletContext();
  27. CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(servletContext);
  28. if (multipartResolver.isMultipart(request)) {
  29. MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
  30. Iterator<String> iterator = multiRequest.getFileNames();
  31. while (iterator.hasNext()) {
  32. MultipartFile multipartFile = multiRequest.getFile(iterator.next());
  33. if (multipartFile != null) {
  34. String parentPath=uploadedFolderFile.getAbsolutePath()+File.separator;
  35. String originalFilename = multipartFile.getOriginalFilename();
  36. String path = parentPath + originalFilename;
  37. File file=new File(path);
  38. multipartFile.transferTo(file);
  39. }
  40. }
  41. }
  42. return "/test";
  43. }
  44. }

部署测试

选择图片后,点击upload上传。

嗯哼,现在来瞅瞅传到服务端的图片

OK!

SpringMVC札集(08)——文件上传的更多相关文章

  1. SSM框架之SpringMVC(5)文件上传

    SpringMVC(5)文件上传 1.实现文件上传的前期准备 1.1.文件上传的必要前提 A form 表单的 enctype 取值必须是: multipart/form-data(默认值是:appl ...

  2. SpringMVC:学习笔记(8)——文件上传

    SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...

  3. SpringMVC注解方式与文件上传

    目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...

  4. SpringMVC 通过commons-fileupload实现文件上传

    目录 配置 web.xml SpringMVC配置文件 applicationContext.xml 文件上传 Controller 上传实现一 上传实现二 测试 依赖 配置 web.xml < ...

  5. springmvc学习笔记--支持文件上传和阿里云OSS API简介

    前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...

  6. SpringMVC 使用MultipartFile实现文件上传(转)

    http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...

  7. SpringMVC源码分析--文件上传

    SpringMVC提供了文件上传的功能,接下来我们就简单了解一下SpringMVC文件上传的开发及大致过程. 首先需要在springMVC的配置文件中配置文件上传解析器 <bean id=&qu ...

  8. SSM-SpringMVC-32:SpringMVC中灌顶传授文件上传

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 我将用自认为最简单的语言,描述Springmvc的文件上传,来将老夫毕生功力灌顶传授给你 首先文件上传,又简至 ...

  9. SpringMVC之单/多文件上传

    1.准备jar包(图标所指必备包,其他按情况导入) 2.项目结构 3.SingleController.java(控制器代码单文件和多文件) package com.wt.uplaod; import ...

随机推荐

  1. 20145303 《Java程序设计》第5周学习总结

    20145303 <Java程序设计>第5周学习总结 教材学习内容总结 1.java中所有错误都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理. ...

  2. spring MVC Action里面怎么设置UTF-8编码集

    /* 编码转换,确保写数据库的时候不会出现乱码 */ public class CodingConvert{ public CodingConvert(){ // } public String to ...

  3. C#生成PDF2019

    因接口生成Pdf推送, 工作需要进行Pdf生成,但网上生成Pdf的文档好少: 1.生成Pdf需要文件路径/内容  都可以配置 2.使用组件 itextsharp.dll 本人用版本:v2.0.5072 ...

  4. # fabirc 配置多组服务器 密码与密钥一起使用 key_filename的设置

    环境说明 myv myv2 是配置在/etc/hosts 的两台 虚拟机 虚拟机ip. 参考英文文档 官方文档的例子不是给的很详细.. http://docs.fabfile.org/en/1.13/ ...

  5. RabbitMQ 安装使用教程

    环境 CentOS7 + Python3.5 yum -y install epel-release erlang socat cd /usr/local/src wget http://www.ra ...

  6. composer安装教程 windows系统 | Linux系统 | mac系统

    如何安装 Composer 下载 Composer 安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 打开命令行并依次执行下列命令安装最新版本的 C ...

  7. mysql中一张(居民)表按年龄段查询数据

    知识点: 用mysql,按年龄段查询一张居民的数据(各年龄段居民的个数) 1.如:查询resident(居民表),按照各年龄段,统计人数 2.mysql语句如下: select ageproporti ...

  8. Examining the Rooms - 第一类斯特灵数

    ---恢复内容开始--- 2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competiti ...

  9. Mysql建表好的例子

    1. DROP TABLE IF EXISTS `sys_warehouse_area`;CREATE TABLE `sys_warehouse_area` ( `id` bigint(20) NOT ...

  10. python 贪婪和非贪婪模式

    这样的正则表达式: r'\*(.+)\*'  如果想要匹配*something*这样的一个串按道理说是没问题的 但是如果文本是*this* is *something* 那么我们的正则表达式就会采取贪 ...