TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
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文件上传方式的更多相关文章
- 基于SpringMVC的文件(增删改查)上传、下载、更新、删除
一.项目背景 摘要:最近一直在忙着项目的事,3个项目过去了,发现有一个共同的业务,那就是附件的处理,附件包括各种文档,当然还有图片等特殊文件,由于时间的关系,每次都是匆匆忙忙的搞定上线,称这项目的空档 ...
- 基于SpringMVC的上传文件实现
基于SpringMVC的上传文件实现 1.项目源码 源码地址:upload 2.关键代码 @RequestMapping("/upload2") public void datal ...
- 18 SpringMVC 文件上传和异常处理
1.文件上传的必要前提 (1)form 表单的 enctype 取值必须是:multipart/form-data(默认值是:application/x-www-form-urlencoded) en ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)
读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...
- springMVC文件上传
参考的地址:http://www.tuicool.com/articles/nMVjaiF 1.需要使用的jar. commons-fileupload.jar与commons-io-1.4.jar二 ...
- springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传
总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- SpringMVC文件上传实现
SpringMVC(注解)上传文件需要注意的几个地方:1.form的enctype="multipart/form-data",这个是上传文件必须的2.applicationCon ...
随机推荐
- JS流程控制语句 来来回回(Do...while循环) 先执行后判断 do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次。
来来回回(Do...while循环) do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次.因为它是先执行代码,后判断条件,如果条件为真,继续循环. do...w ...
- SPOJ - The last digit
https://vjudge.net/problem/SPOJ-LASTDIG 求最后一位,%10就完了 这个题居然要求代码小于等于700B #include <iostream> #in ...
- C++ 标准文件的写入读出(ifstream,ofstream)
ttp://blog.csdn.net/a125930123/article/details/53542261 注: "<<", 插入器,向流输入数据 ...
- LUOGU P3355 骑士共存问题(二分图最大独立集)
传送门 因为骑士只能走"日"字,所以一定是从一个奇点到偶点或偶点到奇点,那么这就是一张二分图,题目要求的其实就是二分图的最大独立集.最大独立集=n-最大匹配. #include&l ...
- wget: command not found 解决方案
wget: command not found 解决方案 wget command not found 解决方案 问题分析 解决方案 方法一yum安装wget 方法二rpm安装 问题分析 安装的是Ce ...
- Taro踩坑记录一: swiper组件pagestate定制,swiperChange中setState导致组件不能滚动。
import Taro, { Component } from '@tarojs/taro'; import { Swiper, SwiperItem, Image, View } from '@ta ...
- webpack 清理旧打包资源插件
当我们修改带hash的文件并进行打包时,每打包一次就会生成一个新的文件,而旧的文件并 没有删除.为了解决这种情况,我们可以使用clean-webpack-plugin 在打包之前将文件先清除,之后再打 ...
- eclipse memory analyzer对系统内存溢出堆文件解析(转)
本文转之:https://blog.csdn.net/rachel_luo/article/details/8992461 前言 性能分析工具之-- Eclipse Memory Analyzer t ...
- 795. Number of Subarrays with Bounded Maximum
数学的方式 是对于所有的字符分成简单的三类 0 小于 L 1 LR 之间 2 大于R 也就是再求 不包含 2 但是包含1 的子数组个数 不包含2的子数组个数好求 对于连续的相邻的n个 非2类数 就有 ...
- 在ALV点击Key值调用TCode,跳过初始屏幕
在开发ALV报表时,通常业务部门会要求在ALV中点击单据号,屏幕跳转到具体业务凭证中查看业务明细,效果如下图: 点击销售销售订单号或者交货单号可传入单据号直接打开销售订单或交货单,实现方式如下: 一. ...