spring 上传文件文件的一个例子,
/**
* 类名称: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 上传文件文件的一个例子,的更多相关文章
- Android+Spring Boot 选择+上传+下载文件
2021.02.03更新 1 概述 前端Android,上传与下载文件,使用OkHttp处理请求,后端使用Spring Boot,处理Android发送来的上传与下载请求.这个其实不难,就是特别多奇奇 ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- spring html5 拖拽上传多文件
注:这仅仅是一个粗略笔记.有些代码可能没用.兴许会再更新一个能够使用的版本号.不足之处,敬请见谅. 1.spring环境搭建,这里使用的是spring3的jar,须要同一时候引入common-IO 和 ...
- spring cloud实战与思考(三) 微服务之间通过fiegn上传一组文件(下)
需求场景: 用户调用微服务1的接口上传一组图片和对应的描述信息.微服务1处理后,再将这组图片上传给微服务2进行处理.各个微服务能区分开不同的图片进行不同处理. 上一篇博客已经讨论了在微服务之间传递一组 ...
- spring上传文件
在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...
- spring boot:单文件上传/多文件上传/表单中多个文件域上传(spring boot 2.3.2)
一,表单中有多个文件域时如何实现说明和文件的对应? 1,说明和文件对应 文件上传页面中,如果有多个文件域又有多个相对应的文件说明时, 文件和说明如何对应? 我们在表单中给对应的file变量和text变 ...
- 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据
在pom.xml中引入: <!--处理2003 excel--> <dependency> <groupId>org.apache.poi</groupId& ...
- springboot整合ueditor实现图片上传和文件上传功能
springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...
- 百度开源富文本编辑器 UEditor配置:图片上传和文件上传独立使用方法
使用UEditor编辑器自带的插件实现图片上传和文件上传功能,这里通过配置UEditor单独使用其内置的第三方插件swfupload来实现图片和文件的上传,通过对UEditor配置轻松实现图片批量上传 ...
- extjs插件开发上传下载文件简单案例
前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...
随机推荐
- permutations(全排列)
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- Oracle——多表查询
本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查询一起完成各个复杂查询的操作: 一 ...
- Java 中遇到null 和为空的情况,使用Optional来解决。
Java 中遇到null 和为空的情况,使用Optional来解决 示例代码: package crazy; import java.util.Optional; class Company { pr ...
- How Microservices are Transforming Python Development
https://blog.appdynamics.com/engineering/how-microservices-are-transforming-python-development/ Summ ...
- Visual Studio Community 2013,功能完整,免费使用
http://www.infoq.com/cn/news/2014/11/VSC2013 微软刚刚宣布了.NET平台的开源计划,与此同时,它还推出了源自Visual Studio Profession ...
- ubuntu11.04安装nginx+php+mysql
先列参考内容,后面我再补充点东西: http://www.4wei.cn/archives/1001436 http://www.gidot.net/blog/article.asp?id=322 上 ...
- 关于HTTP请求、Ajax请求,请求的同步和异步
使用了很长时间的Ajax请求了,一直都是在以异步的方式在使用.昨天听了一个讲座涉及到apache server,偶然想到了这Ajax请求和HTTP请求的一些区别和联系,就在网上好好搜了一顿,把搜到的结 ...
- Java父类对象调用子类实体:方法重写与动态调用
众所周知Java的handle和C++的ponter而不是object对应,我们很熟悉C++的父类pointer调用子类实体的例子,那么对于Java的handle是不是也可以这样呢? 这里我先给一个例 ...
- linux 关于Apache默认编码错误 导致网站乱码的解决方案
Apache默认编码UTF-8在解析A网站的时候没有任何问题,当运行B网站时出现的"蝌蚪文"乱码问题 最近经常有同学在使用LAMP/WAMP时,遇到这样的编码错误问题: A网站 ...
- vue技术分享-你可能不知道的7个秘密
前言 本文是vue源码贡献值Chris Fritz在公共场合的一场分享,觉得分享里面有不少东西值得借鉴,虽然有些内容我在工作中也是这么做的,还是把大神的ppt在这里翻译一下,希望给朋友带来一些帮助. ...