/**

* 类名称:UploadTest 类描述:创建人:zhang 创建时间:2015年3月13日 下午4:20:57 修改人:zhang

* 修改时间:2015年3月13日 下午4:20:57 修改备注:
*
* @version
*
*/
@Controller
public class UploadTest {
@RequestMapping(value = "upFile", method = RequestMethod.POST)
public void upFile(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String dir = null;
if (!file.isEmpty()) {
ServletContext sc = request.getSession().getServletContext();//得到项目的路径
dir = sc.getRealPath("/upload"); // 设定文件保存的目录
//

saveFile01(file, dir);
// saveFile(file, dir);
}

TestInputToString(dir);
// TestInputSream(dir);

}

/**
* 储存上传文件 spring格式的MultipartFile 用MultipartFile 里面的transferTo 方法
*
* @param file
* @param dir
*/
private void saveFile01(MultipartFile file, String dir) {
String filename = file.getOriginalFilename(); // 得到上传时的文件名

File targetFile = new File(dir, filename);

if (!targetFile.exists()) {
targetFile.mkdirs();

}

try {
file.transferTo(targetFile);
} catch (Exception e) {

}
System.out.println("upload over. " + filename);
}

/**
* @param file
* @param dir
* 用DataoutPueStream 储存 MultipartFile类型的文件,
*/
private void saveFile(MultipartFile file, String dir) {
/*************************************/
DataInputStream is = null;
FileOutputStream fos = null;
try {
InputStream in = file.getInputStream();
is = new DataInputStream(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File fil = new File(dir, file.getOriginalFilename());
if (!fil.exists()) {// 如果文件不存在创建文件
fil.mkdirs();
}
try {
fos = new FileOutputStream(fil);

DataOutputStream dos = new DataOutputStream(fos);
byte[] by = new byte[1024];
int data1;
StringBuffer buffer = new StringBuffer();
while ((data1 = is.read(by)) != -1) {
dos.write(by, 0, data1);
buffer.append(data1);
// printStream.write(data1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (is != null) {

is.close();
}
if (fos != null) {
fos.close();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*************************************/
}

/**
* 把输出流的的文件读出来,转化为字符类型 打印出来
*/

private void TestInputToString(String dir) {
BufferedReader br = null;
FileInputStream fip = null;
InputStreamReader isr = null;
try {
fip = new FileInputStream(new File(dir + "/新建文本文档.txt"));

String str;
/*
* 使用 readLine打印文本
*/
isr = new InputStreamReader(fip);// 把字节流转化为字符流
br = new BufferedReader(isr);// 使用字符流缓存区 来提高效率
while ((str = br.readLine()) != null) {
System.out.println(str);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
isr.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/*
* 利用字节把输入流的文件输出来 ByteArrayOutPutSream 是一个数组用来存放字节 数据被写入一个 byte
* 数组。缓冲区会随着数据的不断写入而自动增长
*/
public void TestInputSream(String dir) {

FileInputStream fip = null;
ByteArrayOutputStream ba = null;
DataInputStream os = null;
try {
fip = new FileInputStream(new File(dir + "/新建文本文档.txt"));
ba = new ByteArrayOutputStream();
os = new DataInputStream(fip);
byte[] b = new byte[1024];
int rs;
while ((rs = os.read(b)) != -1) {// 从输入流中读取数据到字节组b中
ba.write(b, 0, rs);// write的方法b是写入的数据,0是偏移量,rs是写入数据的大小
}
System.out.println(ba.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ba != null) {// 关闭输入流,释放资源
try {
ba.close();
os.close();
ba.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}}}

/***************************************/

2.配置文件 spring4-servlet.xml

<!-- 自动扫描controller bean,把作了注解的类转换为bean -->
<mvc:annotation-driven />
<context:component-scan base-package="com.glass.control" />

<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp">
<property name="order" value="0" />
</bean>

<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

spring 上传文件文件的一个例子,的更多相关文章

  1. Android+Spring Boot 选择+上传+下载文件

    2021.02.03更新 1 概述 前端Android,上传与下载文件,使用OkHttp处理请求,后端使用Spring Boot,处理Android发送来的上传与下载请求.这个其实不难,就是特别多奇奇 ...

  2. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  3. spring html5 拖拽上传多文件

    注:这仅仅是一个粗略笔记.有些代码可能没用.兴许会再更新一个能够使用的版本号.不足之处,敬请见谅. 1.spring环境搭建,这里使用的是spring3的jar,须要同一时候引入common-IO 和 ...

  4. spring cloud实战与思考(三) 微服务之间通过fiegn上传一组文件(下)

    需求场景: 用户调用微服务1的接口上传一组图片和对应的描述信息.微服务1处理后,再将这组图片上传给微服务2进行处理.各个微服务能区分开不同的图片进行不同处理. 上一篇博客已经讨论了在微服务之间传递一组 ...

  5. spring上传文件

    在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...

  6. spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)

    一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...

  7. 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据

    在pom.xml中引入: <!--处理2003 excel--> <dependency> <groupId>org.apache.poi</groupId& ...

  8. springboot整合ueditor实现图片上传和文件上传功能

    springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...

  9. 百度开源富文本编辑器 UEditor配置:图片上传和文件上传独立使用方法

    使用UEditor编辑器自带的插件实现图片上传和文件上传功能,这里通过配置UEditor单独使用其内置的第三方插件swfupload来实现图片和文件的上传,通过对UEditor配置轻松实现图片批量上传 ...

  10. extjs插件开发上传下载文件简单案例

    前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...

随机推荐

  1. 初步认识thymeleaf:简单表达式和标签(二)

    1.th:each:循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size, ...

  2. MVC3 项目总结

    验证 Validation 多样化验证规则 http://www.cnblogs.com/xling/archive/2012/07/11/2587002.html 最常见的验证方式是:在实体的属性上 ...

  3. Python实现PPPOE攻击工具

    前言 大家可能对PPPOE不是很熟悉,但是肯定对拨号上网非常熟悉,拨号上网就是用的这种通信协议.一般PPPOE认证上网主要用于校园网或者小区网中,拨号界面如下图所示. 但是PPPOE这种通信协议,是有 ...

  4. Docker快速入门(二)

    上篇文章<Docker快速入门(一)>介绍了docker的基本概念和image的相关操作,本篇将进一步介绍image,容器和Dockerfile. 1 image文件 (1)Docker ...

  5. Python flask中的配置

    当你开始学习Flask时,配置看上去是小菜一碟.你仅仅需要在config.py定义几个变量,然后万事大吉. 然而当你不得不管理一个生产上的应用的配置时,这一切将变得棘手万分. 你不得不设法保护API密 ...

  6. php获取指定目录下的所有文件列表

    在我们实际的开发需求中,经常用到操作文件,今天就讲一下关于获取指定目录下的所有文件的几种常用方法: 1.scandir()函数 scandir() 函数返回指定目录中的文件和目录的数组. scandi ...

  7. Python_字符串之删除空白字符或某字符或字符串

    ''' strip().rstrip().lstrip()分别用来删除两端.右端.左端.连续的空白字符或字符集 ''' s='abc ' s2=s.strip() #删除空白字符 print(s2) ...

  8. tensorflow 1.8, ubuntu 16.04, cuda 9.0, nvidia-390,安装踩坑指南。

    被tensorflow 1.8, ubuntu 16.04, cuda 9.0, nvidia-390折磨了5天,终于上坑,留下指南,造福后人. 1.先把依赖搞清楚: tensorflow 1.8依赖 ...

  9. hexo+github创建属于自己的博客

    配置环境 安装Node(必须) 作用:用来生成静态页面的 到Node.js官网下载相应平台的最新版本,一路安装即可. 安装Git(必须) 作用:把本地的hexo内容提交到github上去. 安装Xco ...

  10. PAT1088:Rational Arithmetic

    1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...