自定义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)——文件上传的更多相关文章

  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. 20145321 《Java程序设计》第2周学习总结

    20145321 <Java程序设计>第2周学习总结 教材学习内容总结 一.类型.变量.运算符 1.类型(基本类型) (1)整数:short(占2字节),int(占4字节),long(占8 ...

  2. Git笔记之初识vi编辑器

    1.vi编辑器 如同Windows下的记事本,vi编辑器是Linux下的标配,通过它我们可以创建.编辑文件.它是一个随系统一起安装的文本编辑软件. vi编辑器提供了3种模式,分别是命令模式.插入模式. ...

  3. LeetCode (226):Invert Binary Tree 递归实现

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  4. Centos为mysql开启binlog

    1.查询mysql配置文件所在位置 2.编辑配置文件/etc/my.cnf 在文件尾部添加: log-bin=/var/lib/mysql/mysql-bin server-id=123454  (5 ...

  5. git 总结命令

    git 命令 创建git版本库:git init 查看状态:git status 把文件添加到暂存区:git add 把文件提交到版本库:git commit  -m "提交说明" ...

  6. Spring Boot企业微信点餐系统

    第1章 课程介绍 包括项演示.课程概述.课程安排.学习前提等的介绍, 让同学们了解这课程 1-1 课程介绍 第2章 项目设计 包括需求分析,项?目设计,项?目架构,数据库设计等等. 2-1 项目设计 ...

  7. Hue的安装与部署

    Hue的安装与部署 hadoop hue Hue 简介 Hue是一个开源的Apache Hadoop UI系统,最早是由Cloudera Desktop演化而来,由Cloudera贡献给开源社区,它是 ...

  8. 第十一篇:Spark SQL 源码分析之 External DataSource外部数据源

    上周Spark1.2刚发布,周末在家没事,把这个特性给了解一下,顺便分析下源码,看一看这个特性是如何设计及实现的. /** Spark SQL源码分析系列文章*/ (Ps: External Data ...

  9. 模仿某旅行网站 纯css实现背景放大效果

    基本功能是鼠标移动到图片上,对应宽度变宽.其中布局和基本样式直接copy官网,功能部分是自己瞎鼓捣实现的. 直接上代码: HTML部分 <div class="fold_wrap&qu ...

  10. GEO--工具 ScanGEO

    http://scangeo.dartmouth.edu/ScanGEO/ ScanGEO - parallel mining of high-throughput gene expression d ...