自定义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. linux及安全第二周总结——20135227黄晓妍

    实验部分: 首先运行结果截图 代码分析: Mypcb.h /* *  linux/mykernel/mypcb.h * *  Kernel internal PCB types * *  Copyri ...

  2. ARTS Week 001

    Algorithm Leetcode 1. Two Sum Given an array of integers, return indices of the two numbers such tha ...

  3. COJS:1829. [Tyvj 1728]普通平衡树

    ★★★   输入文件:phs.in   输出文件:phs.out   简单对比 时间限制:1 s   内存限制:128 MB [题目描述] 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需 ...

  4. Linux系统巡检项目

    系统检测 1.检查系统类型 2.检查发行版本 3.检查内核版本 4.检查主机名称 5.检查是否启用SElinux 6.检测默认的语言/编码 7.检测uptime 8.检测最后启动时间等 CPU检查 1 ...

  5. PAT L3-012 水果忍者

    占个坑,等自己数学好一点以后再来重新把这个题写一遍 附上链接 附上大牛代码: #include <stdio.h> #include <algorithm> #define I ...

  6. Flume实例一学习

    cp conf/flume-env.sh.template conf/flume-env.sh 打开flume-env.sh,配置Java环境变量 [root@test1 apache-flume-- ...

  7. 05_MySQL常见函数_分组函数

    # 分组函数/*功能: 统计,又称为聚合函数,统计函数,组函数 传入一组值,统计后得到一个值 分类: sum 求和,avg 平均值,max 最大值,min 最小值,count 计算个数 特点: 1. ...

  8. 03_Storm编程上手-wordcount

    1. Storm编程模型概要 消息源spout, 继承BaseRichSpout类 或 实现IRichSpout接口1)BaseRichSpout类相对比较简单,需要覆写的方法较少,满足基本业务需求2 ...

  9. 仿照Chome的GhostPage调试功能

    今天在测试过程中发现了网站的一个bug,在大屏幕上是自适应的,小屏幕笔记本上高度不是自适应,html的高度并不是浏览器的高度,小屏幕总是差了一截,在调试过程中偶然发现差的那一小截正好是一个横向滑动条的 ...

  10. Mahout 0.10.1安装(Hadoop2.6.0)及Kmeans测试

    1.版本和安装路径 Ubuntu 14.04 Mahout_Home=/opt/mahout-0.10.1 Hadoop_Home=/usr/local/hadoop Mavent_Home=/opt ...