/**

* 类名称: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. 80端口被NT kernel & System 占用

    新年后正常上班的第一天,客户报告,虚拟机上的网站起不来了. 登录虚拟机的远程桌面,闪几下连接信息,后面就没了,不显示远程桌面.联系虚拟机管理,重启,远程桌面是连上了,网站还是起不来. 查看window ...

  2. JAVA堆栈的区别

    1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆.              2. 栈的优势是,存取速度 ...

  3. 前端技术之_CSS详解第六天--完结

    前端技术之_CSS详解第六天--完结 一.复习第五天的知识 a标签的伪类4个: a:link 没有被点击过的链接 a:visited 访问过的链接 a:hover 悬停 a:active 按下鼠标不松 ...

  4. classes目录中没有class文件的一个原因

    可能是你的build设置有问题:比如本来有的jar被删除的情况下.build不会报错,但是classes目录下什么都没有.

  5. Pascal Triangle

    Description: Given numRows, generate the first numRows of Pascal's triangle. For example, given numR ...

  6. SOFA 源码分析 — 泛化调用

    前言 通常 RPC 调用需要客户端使用服务端提供的接口,而具体的形式则是使用 jar 包,通过引用 jar 包获取接口的的具体信息,例如接口名称,方法名称,参数类型,返回值类型. 但也存在一些情况,例 ...

  7. 在python中单线程,多线程,多进程对CPU的利用率实测以及GIL原理分析

    首先关于在python中单线程,多线程,多进程对cpu的利用率实测如下: 单线程,多线程,多进程测试代码使用死循环. 1)单线程: 2)多线程: 3)多进程: 查看cpu使用效率: 开始观察分别执行时 ...

  8. Maven Scope 依赖范围

    Maven依赖范围就是用来控制依赖与这三种classpath(编译classpath.测试classpath.运行classpath)的关系,Maven有以下几种依赖范围: ·compile:编译依赖 ...

  9. Python_小学口算题库生成器

    import random import os import tkinter import tkinter.ttk from docx import Document columnsNumber = ...

  10. 对C#热更新方案ILRuntime的探究

    转载请标明出处:http://www.cnblogs.com/zblade/ 对于游戏中的热更,目前主流的解决方案,分为Lua(ulua/slua/xlua/tolua)系和ILRuntime代表的c ...