1.传统文件上传方式

<!-- 文件上传需要的jar -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

  1>form.jsp配置

  设置编码方式为:多部分表格-数据

<form action="fileUpload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"><br>
<input type="submit" value="传统方式">
</form>

   2>uploadController

/**
* 传统模式的文件上传
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/fileUpload")
public String testFileUpload(HttpServletRequest request) throws Exception { String path = request.getSession().getServletContext().getRealPath("/upload");
File file = new File(path);
System.out.println("path:" + path);
if (!file.exists()) {
file.mkdir();
} // 解析request对象,获取上传文件项
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
// 获得enctype="multpart/form-data"的表单项 for (FileItem item : items) {
if (item.isFormField()) {
} else {
// 获取上传文件想项
String fileName = item.getName();
                String name = UUID.randomUUID().toString().replace("-", "")+fileName;
         item.write(new File(file, name));
// 在内存中删除临时文件
item.delete();
}
}
return "success";
}

2.SpringMVC文件上传

  1>form.jsp配置

  设置编码方式为:多部分表格-数据

<form action="fileUpload2" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"><br> //name必须时controller就收的参数名
<input type="submit" value="SpringMVC方式">
</form>

  2>配置上传文件的文件解析器

<!--配置上传文件的文件解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"></property>
</bean>

  3>uploadController

public class UserController {

    /**
* Springmvc模式的文件上传
*
* @param model
* @return
* @throws Exception
*/
@RequestMapping("/fileUpload2")
public String testFileUpload2(MultipartFile upload, HttpServletRequest request) throws Exception { String path = request.getSession().getServletContext().getRealPath("/upload");
File file = new File(path);
System.out.println("path:" + path);
if (!file.exists()) {
file.mkdir();
} // 获取上传文件想项
 String name = UUID.randomUUID().toString().replace("-", "")+upload.getOriginalFilename();
upload.transferTo(new File(path, name));
return "success";
}

                                                           

                                                          ————你是我自罚三杯也不肯开口的秘密

TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式的更多相关文章

  1. 基于SpringMVC的文件(增删改查)上传、下载、更新、删除

    一.项目背景 摘要:最近一直在忙着项目的事,3个项目过去了,发现有一个共同的业务,那就是附件的处理,附件包括各种文档,当然还有图片等特殊文件,由于时间的关系,每次都是匆匆忙忙的搞定上线,称这项目的空档 ...

  2. 基于SpringMVC的上传文件实现

    基于SpringMVC的上传文件实现 1.项目源码 源码地址:upload 2.关键代码 @RequestMapping("/upload2") public void datal ...

  3. 18 SpringMVC 文件上传和异常处理

    1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...

  4. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  5. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  6. springMVC文件上传

    参考的地址:http://www.tuicool.com/articles/nMVjaiF 1.需要使用的jar. commons-fileupload.jar与commons-io-1.4.jar二 ...

  7. springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传

    总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...

  8. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

  9. SpringMVC文件上传实现

    SpringMVC(注解)上传文件需要注意的几个地方:1.form的enctype="multipart/form-data",这个是上传文件必须的2.applicationCon ...

随机推荐

  1. Python对象和类

    Python 里的所有数据都是以对象形式存在的,对象是类的实例. 定义类(class) 使用class来定义一个类. 比如,定义一个cat类,如下: class Cat(): def __init__ ...

  2. LINUX挂接移动硬盘

    对linux系统而言,USB接口的移动硬盘是当作SCSI设备对待的.插入移动硬盘之前,应先用fdisk –l 或 more /proc/partitions查看系统的硬盘和硬盘分区情况. [root ...

  3. JS规则 我与你同在(逻辑与操作符)数学中的“b大于a,b小于c”是“a<b<c”,那么在JavaScript中可以用&&表示

    我与你同在(逻辑与操作符) 数学里面的"a>b",在JavaScript中还表示为a>b:数学中的"b大于a,b小于c"是"a<b& ...

  4. UNIX环境高级编程------apue.h找不到

    运行1-3代码时,出现问题:apue.h 没有找到问题 1.去此网址下载源码: http://www.apuebook.com/code3e.html 压缩包名为:src.3e.tar.gz 2.解压 ...

  5. css---4表单相关伪类

    input:enabled{ color:red;} input:disabled{ color:blue;} enabled or disable 表单的状态 input:checked{ widt ...

  6. UOJ450 复读机

    题意:n个位置,k种颜色.求有多少种方案使得每种颜色恰出现d的倍数次. 解:d=1就快速幂,n,k很小就DP,记得乘组合数来分配位置. d = 2 / 3的时候,考虑生成函数. f(x) = ∑[d ...

  7. php中的线程、进程和并发区别

    https://mp.weixin.qq.com/s/Ps5w13TTmpnZx-RPWbsl1A 进程 进程是什么?进程是正在执行的程序:进程是正在计算机上执行的程序实例:进程是能分配给处理器并由处 ...

  8. Android基础控件ProgressBar进度条的使用

    1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...

  9. PAT甲级——A1091 Acute Stroke【30】

    One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...

  10. python3-常用模块之time

    import time time模块主要是处理各种类型的时间 常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行,单位为秒. 2.time.time() 获取当前时间戳 时间戳 ...