/**

* 类名称: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. rcp perspective 添加9个视图均匀排列

    PageLayout布局方法 pageLayout.addView("NodePMTSStream" + ":1", IPageLayout.TOP, 0.5f ...

  2. postgresql to_char 问题

    select create_time from  xxx; select to_char(create_time,'yyyy-MM-dd HH24:mm:ss') as  create_time fr ...

  3. SQL Server Agent Job 多服务器管理

  4. linux上nginx新建站点

    遇到一个要将后台部分模块剥离出来,重新放到一个新的后台上的问题: 这样一来,就要在服务器上新建站点,but,服务器是linux系统的,不是很熟,经过多方努力,搞定了 在这记录一下,用到的linux命令 ...

  5. Django模型层之字段查询参数及聚合函数

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 字段查询是指如何指定SQL WHERE子句的 ...

  6. Maven项目管理工具

    Maven项目管理工具 白面郎君 Svn eclipse maven量级 1 Maven的简介 1.1 什么是maven 是apache下的一个开源项目,是纯java开发,并且只是用来管理java项目 ...

  7. 解决jequry使用keydown无法跳转的问题

    问题描述 代码 <script> $("document").ready(function() { $("#button").click(funct ...

  8. esayui扩展验证方法

    下面是关于平时中积累的esayui扩展验证方法仅作记录:       /**************************************************************** ...

  9. JVM配置参数详解(目前不够完善)

    最近看了有关虚拟机的书籍,发现有很多虚拟机配置参数不知道,特来记录一下, -XX: MaxDirectMemorySize--->设置直接内存,不设置与Java堆内存最大值一致 -XX:Perm ...

  10. PAT1121:Damn Single

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...