用SpringMVC实现的上传下载
1、导入相关jar包
commons-fileupload.jar
commons-io.jar
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC_fileUpLoad</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> </web-app>
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.action" method="post"enctype="multipart/form-data">
<input type="file" name="pic" />
<input type="submit" value="上传" />
</form>
</body>
</html>
4、配置springmvc-servlet.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="com.etc"></context:component-scan> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400"/>
</bean> <bean id="fileUpLoadController" class="com.etc.fileupload.FileUpLoadController"/>
</beans>
5、编写handler处理器
package com.etc.fileupload; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; @Controller
public class FileUpLoadController { @RequestMapping("/upload.action")
public String UpLoad(@RequestParam MultipartFile pic,Model model) throws IOException{ //上传
if(pic!=null||!pic.isEmpty()){
byte[] b = pic.getBytes();
// System.out.println(pic.getOriginalFilename()); //123.jpg
// System.out.println(pic.getName()); //pic
// System.out.println(pic.getContentType()); //image/jpeg FileOutputStream out=new FileOutputStream("E:"+File.separator+"images"+File.separator+pic.getOriginalFilename());
out.write(b);
model.addAttribute("filename",pic.getOriginalFilename());
}
return "uploadsus.jsp";
} @RequestMapping("/down.action")
public ResponseEntity<byte[]> downFile(HttpServletRequest r,String filename) throws IOException{ //下载
//配置http请求头文件信息 HttpHeaders headers = new HttpHeaders();
String path=r.getServletContext().getRealPath("/img")+ File.separator + filename; File file=new File(path); headers.setContentDispositionFormData("attachment",filename);//不自动打开
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //头文件内容类型
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
}
6、编写下载页面
<%@ 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>
<h1>图片上传成功</h1>
<img src="img/${filename}" alt="图片" width="200px"/> <br/><a href="down.action?filename=${filename}">下载</a>
</body>
</html>
用SpringMVC实现的上传下载的更多相关文章
- 用SpringMVC实现的上传下载方式二(多文件上传)
参考来源: http://blog.csdn.net/qq_32953079/article/details/52290208 1.导入相关jar包 commons-fileupload.j ...
- springMVC实现文件上传下载
上传文件和下载文件是个常用的技能,在哪里开发几乎都能遇见,而所有的上传控件各不相同,插件很多,后台也有很多,这里我只尝试过这个方法觉的还够简洁.具体如下实现: 1.spring-mvc.xml配置 ...
- SSM框架-SpringMVC 实例文件上传下载
一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...
- 14.SpringMVC之文件上传下载
SpringMVC通过MultipartResolver(多部件解析器)对象实现对文件上传的支持. MultipartResolver是一个接口对象,需要通过它的实现类CommonsMultipart ...
- SpringMVC的文件上传下载,异常处理,拦截器的小总结
文件的上传和下载 我们通常在访问网页时会使用到文件的上传与下载的功能,那么他是如何实现的呢? 1 下载: ResponseEntity :用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览 ...
- SpringMVC异步文件上传下载
首先了解一下File的构造方法: File(String pathname):根据一个路径得到File对象 File(String parent,String child):根据一个目录和一个子文件/ ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
- springmvc 上传下载
springmvc文件上传下载在网上搜索的代码 参考整理了一份需要使用的jar.commons-fileupload.jar与commons-io-1.4.jar 二个文件 1.表单属性为: enct ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
随机推荐
- 几点平时不太注意的CSS知识
1:文本显示的时候,我们发现左右参差不齐,text-align:justify 就能让文本左右都齐刷刷的啦: 2:input标签的内容,处于安全考虑,有时候我们并不希望别人黏贴复制,这时候这样干:& ...
- M2Crypto
M2Crypto = Python + OpenSSL + SWIG M2Crypto is a crypto and SSL toolkit for Python. 上面是M2Crypto的READ ...
- spark开发环境配置
以后spark,mapreduce,mpi可能三者集于同一平台,各自的侧重点有所不用,相当于云计算与高性能计算的集合,互补,把spark的基础看了看,现在把开发环境看看,主要是看源码,最近Apache ...
- component and slot
component and slot 使用: 1.component panel <article class="message"> <div class=&qu ...
- 链表倒置,这个还是考验仔细程度,第一遍还没做对 —— 剑指Offer
https://www.nowcoder.net/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage= ...
- ImportError: No module named MySQLdb解决办法
http://blog.slogra.com/post-429.html http://blog.sina.com.cn/s/blog_74a7e56e0101a7qy.html 今天突发奇想在服务器 ...
- no matching function transform?
http://stackoverflow.com/questions/19876746/stdtolower-and-visual-studio-2013 http://forums.codeguru ...
- Maven具体解释之仓库------本地仓库、远程仓库
在Maven中,不论什么一个依赖.插件或者项目构建的输出.都能够称之为构件. Maven在某个统一的位置存储全部项目的共享的构件.这个统一的位置.我们就称之为仓库.(仓库就是存放依赖和插件的地方) 不 ...
- C++进阶之虚函数表
C++通过继承(inheritance)和虚函数(virtual function)来实现多态性.所谓多态,简单地说就是,将基类的指针或引用绑定到子类的实例,然后通过基类的指针或引用调用实际子类的成员 ...
- oracle initialization or shutdown in progress 问题解决
今天登录oracle时遇到oracle initialization or shutdown in progress 这个错误提示,在网上搜了下,试了非常多方法,最后结合几种方法结合,成功攻克了问题! ...