springmvc文件上传下载
在网上搜索的代码 参考整理了一份
需要使用的jar、
commons-fileupload.jar与commons-io-1.4.jar 二个文件 1、表单属性为: enctype="multipart/form-data"
2、springmvc配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 扫描包 -->
<context:component-scan base-package="com.ai.customer" />
<!-- 启动注解 -->
<mvc:annotation-driven />
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为10MB -->
<property name="maxUploadSize">
<value>10000000</value>
</property>
</bean>
<!-- 静态文件访问 -->
<mvc:default-servlet-handler/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2、上传下载功能代码
package com.ai.customer.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FileUploadController {
/*
* SpringMVC中的文件上传
* @第一步:由于SpringMVC使用的是commons-fileupload实现,故将其组件引入项目中
* @这里用到的是commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
* @第二步:spring-mvx中配置MultipartResolver处理器。可在此加入对上传文件的属性限制
* <bean id="multipartResolver"
* class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
* <!-- 设置上传文件的最大尺寸为10MB -->
* <property name="maxUploadSize">
* <value>10000000</value>
* </property>
* </bean>
* 第三步:在Controller的方法中添加MultipartFile参数。该参数用于接收表单中file组件的内容
*第四步:编写前台表单。注意enctype="multipart/form-data"以及<input type="file"
name="****"/>
* 如果是单个文件 直接使用MultipartFile 即可
*/
@RequestMapping("/upload.do")
public ModelAndView upload(String name,
//上传多个文件
@RequestParam("file") MultipartFile[] file,
HttpServletRequest request) throws IllegalStateException,
IOException {
//获取文件 存储位置
String realPath = request.getSession().getServletContext()
.getRealPath("/uploadFile");
File pathFile = new File(realPath);
if (!pathFile.exists()) {
//文件夹不存 创建文件
pathFile.mkdirs();
}
for (MultipartFile f : file) {
System.out.println("文件类型:"+f.getContentType());
System.out.println("文件名称:"+f.getOriginalFilename());
System.out.println("文件大小:"+f.getSize());
System.out.println(".................................................");
//将文件copy上传到服务器
f.transferTo(new File(realPath + "/" + f.getOriginalFilename()));
//FileUtils.copy
}
//获取modelandview对象
ModelAndView view = new ModelAndView();
view.setViewName("redirect:index.jsp");
return view;
}
@RequestMapping(value = "download.do")
public ModelAndView download(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// String storeName = "Spring3.xAPI_zh.chm";
String storeName="房地.txt";
String contentType = "application/octet-stream";
FileUploadController.download(request, response, storeName, contentType);
return null;
}
//文件下载 主要方法
public static void download(HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType
) throws Exception {
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//获取项目根目录
String ctxPath = request.getSession().getServletContext()
.getRealPath("");
//获取下载文件露肩
String downLoadPath = ctxPath+"/uploadFile/"+ storeName;
//获取文件的长度
long fileLength = new File(downLoadPath).length();
//设置文件输出类型
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(storeName.getBytes("utf-8"), "ISO8859-1"));
//设置输出长度
response.setHeader("Content-Length", String.valueOf(fileLength));
//获取输入流
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
//输出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
//关闭流
bis.close();
bos.close();
}
}
3、上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="text" name="name" />
<br>
<input type="file" name="file">
<br>
<input type="file" name="file" />
<input type="submit" value="提交">
</form>
</body>
</html>
4、下载直接访问控制器如:http:\\localhost:8080/springmvc/download.do

springmvc 上传下载的更多相关文章

  1. SpringMVC 上传下载 异常处理

    SpringMVC 上传下载 异常处理 上一章节对SpringMVC的表单验证进行了详细的介绍,本章节介绍SpringMVC文件的上传和下载(重点),国际化以及异常处理问题.这也是SpringMVC系 ...

  2. SpringMVC上传下载

    springmvc上传和下载功能 写在一个简单的示例在线基准码 1.导入的必要性jar包:ant.jar.commons-fileupload.jar.connom-io.jar. 当然spring ...

  3. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  4. SpringMVC文件上传下载

    不多说,代码: Spring-config.xml<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件, 如果扫描到有Spring的相关注解的类,则把这些类注 ...

  5. SSM框架之中如何进行文件的上传下载

    SSM框架的整合请看我之前的博客:http://www.cnblogs.com/1314wamm/p/6834266.html 现在我们先看如何编写文件的上传下载:你先看你的pom.xml中是否有文件 ...

  6. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

  7. SpringMVC 文件上传下载

    目录 文件上传 MultipartFile对象 文件下载 上传下载示例 pom.xml增加 创建uploadForm.jsp 创建uploadForm2.jsp 创建userInfo.jsp spri ...

  8. SocketIo+SpringMvc实现文件的上传下载

    SocketIo+SpringMvc实现文件的上传下载 socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示: GIT地址 ...

  9. SpringMVC整合fastdfs-client-java实现web文件上传下载

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...

随机推荐

  1. jquer 事件,选择器,dom操作

    一.jQuery简介 jQuery 是一个 JavaScript 库.(其实就是js,就是封装了,语法上有些不一样) jQuery 极大地简化了 JavaScript 编程. jQuery 库位于一个 ...

  2. ASP.NET MVC过滤器

    在ASP.NET MVC中有个重要特性就是过滤器,使得我们在MVC程序开发中更好的控制浏览器请求的URL,不是每个请求都有响应内容,只有特定得用户才有.园子里关于过滤器的资料也有很多,这篇文章主要是记 ...

  3. C#使用资源文件的方法

    其实,对于资源文件的使用,说白点就是通过强制类型转换,将资源文件里的数据强行的转换成你需要的(换种方式说,就是你原来存进去什么,就用什么类型拿出来). 主要通过System.Resources.Res ...

  4. SignalR入门篇

    写在前面的废话 在写关于SignalR的学习笔记之前研究了几天的webSocket,毕竟这才是未来的技术趋势,虽然很早就听说过WebSocket,但是并没有在实际项目中遇到过,所以也就没有深入研究.通 ...

  5. Java代码优化(长期更新)

    前言 2016年3月修改,结合自己的工作和平时学习的体验重新谈一下为什么要进行代码优化.在修改之前,我的说法是这样的: 就像鲸鱼吃虾米一样,也许吃一个两个虾米对于鲸鱼来说作用不大,但是吃的虾米多了,鲸 ...

  6. 安卓模拟器genymotion连接eclipse成功但是不显示其中项目

    安卓模拟器困了我两三天了,原装模拟器比较慢,忍受不了,查到安卓模拟器的神器——genymotion 按照网上的步骤一步步都安装完毕,最后打开后发现,genymotion界面里面没有找到新建的工程, 这 ...

  7. linux下tomcat安全配置

    转:http://www.tuicool.com/articles/R7fQNfQ 0x00 删除默认目录 安装完tomcat后,删除 $CATALINA_HOME/webapps 下默认的所有目录文 ...

  8. 轻松掌握:JavaScript模板方法模式

    模板方法模式 假如我们有一些对象,各个对象之间有一些相同的行为,也有一些不同的行为,这时,我们就可以用模板方法模式来把相同的部分上移到它们的共同原型中(父类),而将不同的部分留给自己各自重新实现. 模 ...

  9. Clion 跨平台的C++ IDE

    CLion 是 JetBrains 推出的全新的 C/C++ 跨平台集成开发环境. 正式版本已经发出,目前是1.0.1 http://www.jetbrains.com/clion/ http://b ...

  10. iOS之App加急审核详细步骤

    申请加急网址:https://developer.apple.com/appstore/contact/appreviewteam/index.html 补充:加急审核说明是可以写中文的 提交加急审核 ...