spring是一个很强大的框架,使用它可以完成几乎所有的功能,对于图片的上传与下载也挺方便的。要实现文件的上传,必须在SpringMVC框架的核心配置文件中注册文件上传解析器,并且,为了解决中文名字的问题,要将文件解析器中的defaultEncoding属性设置为utf-8.
首先编写简单的jsp页面:对于上传的表单我们要在表单属性中加入属性: enctype="multipart/form-data"
%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.js"></script>
</head>
<body>
<form action="${pageContext.request.contextPath }/springmvc/fileUpload" method="POST" enctype="multipart/form-data">
<input type="file" name="imgs">
<input type="file" name="imgs">
<input type="file" name="imgs">
<input type="submit" value="提交">
</form>
<a href="${pageContext.request.contextPath }/springmvc/fileDownload">下载</a>
</body>
</html>
对于控制器这样写:
/**
接下来就是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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册开发中要进行注册组件扫描器 -->
<context:component-scan base-package="com.lianxi.handlers"></context:component-scan>
<!-- 注解开发第二步,注册注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图层前缀 -->
<property name="prefix" value="/jsp/"></property>
<!-- 视图层后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 注册文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!-- 解决静态资源第二种方式 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- 解决静态资源第二种方式 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>
web.xml文件的配置就不列出来了,这样就可以实现文件的简单的上传与下载的功能。