SpringMVC多文件上传zxw文档格式.docx
《SpringMVC多文件上传zxw文档格式.docx》由会员分享,可在线阅读,更多相关《SpringMVC多文件上传zxw文档格式.docx(22页珍藏版)》请在冰点文库上搜索。
pageEncoding="
UTF-8"
%>
%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"
:
//"
+request.getServerName()+"
"
+request.getServerPort()+path+"
/"
;
!
DOCTYPEHTMLPUBLIC"
-//W3C//DTDHTML4.01Transitional//EN"
>
html>
<
head>
basehref="
%=basePath%>
title>
MyJSP'
index.jsp'
startingpage<
/title>
metahttp-equiv="
pragma"
content="
no-cache"
cache-control"
expires"
0"
keywords"
keyword1,keyword2,keyword3"
description"
Thisismypage"
scripttype="
text/javascript"
src="
jquery-1.7.1.min.js"
/script>
i=1;
j=1;
$(document).ready(function(){
$("
#btn_add1"
).click(function(){
document.getElementById("
newUpload1"
).innerHTML+='
divid="
div_'
+i+'
inputname="
file"
type="
/>
inputtype="
button"
value="
删除"
onclick="
del_1('
)"
/>
/div>
'
i=i+1;
});
#btn_add2"
newUpload2"
+j+'
file_'
del_2('
j=j+1;
functiondel_1(o){
).removeChild(document.getElementById("
div_"
+o));
}
functiondel_2(o){
/head>
body>
h1>
springMVC字节流输入上传文件<
/h1>
formname="
userForm1"
action="
file/upload.html"
enctype="
multipart/form-data"
method="
post"
name="
id="
btn_add1"
增加一行"
>
submit"
上传"
/form>
br>
hralign="
left"
width="
60%"
color="
#FF0000"
size="
3"
springMVC包装类上传文件<
userForm2"
file/upload2.html"
btn_add2"
h1style="
color:
#F00"
以上面的多文件上传为准,下面的是草稿<
ahref="
stratCon/start.html"
第一步:
开始访问springMVC和spring集成<
/a>
br/>
普通文件上传<
form1"
stratCon/upload1.html"
选择文件:
fileName"
<
优化后的文件上传<
form2"
stratCon/upload2.html"
fileName2"
fileName3"
/html>
4、success.jsp首页内容:
成功页面!
/body>
5、web.xml内容:
?
xmlversion="
1.0"
encoding="
web-appversion="
3.0"
xmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
display-name>
/display-name>
--加载顺序:
context-param>
listener>
filter>
servlet
context-param>
<
param-name>
contextConfigLocation<
/param-name>
param-value>
classpath:
spring/spring-core.xml<
/param-value>
/context-param>
spring配置
listener>
listener-class>
org.springframework.web.context.ContextLoaderListener<
/listener-class>
/listener>
-->
--第三步:
加载解析初始化Spring配置文件(利用总分配器DispatcherServlet处理请求到Spring配置文件)-->
servlet>
servlet-name>
User<
/servlet-name>
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
--读取Spring配置文件,加载解析初始化(默认查找路径:
/WEB-INF/[servlet-name]-servlet.xml)-->
init-param>
<
--查找spring配置文件路径(不配置init-param的话,默认为/WEB-INF/下Login-servlet.xml)-->
spring/User-servlet.xml<
--classpath路径为:
\WebRoot\WEB-INF\classes-->
/init-param>
load-on-startup>
1<
/load-on-startup>
--tomcat启动时加载解析DispatcherServlet类(如果不设置启动,则第一次访问时启动,比较慢)-->
/servlet>
--第二步:
index页面请求映射-->
servlet-mapping>
url-pattern>
*.html<
/url-pattern>
/servlet-mapping>
--配置编码格式过滤器-->
filter>
filter-name>
EncodingFilter<
/filter-name>
filter-class>
org.springframework.web.filter.CharacterEncodingFilter<
/filter-class>
encoding<
UTF-8<
--初始化编码是UTF-8-->
forceEncoding<
true<
--强制转换response和request.setCharacterEncoding("
)编码格式-->
/filter>
--过滤器映射-->
filter-mapping>
/*<
--过滤所有请求-->
/filter-mapping>
welcome-file-list>
welcome-file>
index.jsp<
/welcome-file>
/welcome-file-list>
/web-app>
6、spring配置文件User-servlet.xml内容:
beansxmlns="
//www.springframework.org/schema/beans"
aop="
//www.springframework.org/schema/aop"
context="
//www.springframework.org/schema/context"
p="
//www.springframework.org/schema/p"
tx="
//www.springframework.org/schema/tx"
mvc="
//www.springframework.org/schema/mvc"
http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-3.0.xsd
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd"
--第四步:
启动注解组件扫描加载指定包下面的注解(需包含头部的:
xmlns、xmlns:
context、xmlns:
mvc、xsi:
schemaLocation)-->
context:
component-scanbase-package="
com.test"
--第八步:
利用视图解析器继承扩展UrlBasedViewResolver的子类InternalResourceViewResolver解析返回的模型页面-->
beanclass="
org.springframework.web.servlet.view.InternalResourceViewResolver"
--<
propertyname="
viewClass"
org.springframework.web.servlet.view.JstlView"
InternalResourceViewResolver解析器可以解析jstl所以不需要此viewClass-->
prefix"
/WEB-INF/jsp/"
--/表示WebContent下,/WEB-INF/jsp/此路径下文件安全性高,不可直接访问-->
suffix"
.jsp"
--查找文件的后缀-->
/bean>
--springmvc文件上传配置-->
beanid="
multipartResolver"
class="
mons.CommonsMultipartResolver"
defaultEncoding"
utf-8"
maxUploadSize"
10485760000"
maxInMemorySize"
40960"
/beans>
7、UploadController类内容:
packagecom.test;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Date;
importjava.util.Iterator;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.multipart.MultipartFile;
importorg.springframework.web.multipart.MultipartHttpServletRequest;
importmons.CommonsMultipartFile;
importmons.CommonsMultipartResolver;
@Controller
@RequestMapping("
/file"
)
publicclassUploadController{
@RequestMapping("
/upload"
)
publicStringaddUser(@RequestParam("
)CommonsMultipartFile[]files,HttpServletRequestrequest){
intpre=(int)System.currentTimeMillis();
for(inti=0;
i<
files.length;
i++){
System.out.println("
fileName---------->
+files[i].getOriginalFilename());
if(!
files[i].isEmpty()){
try{
//拿到输出流,同时重命名上传的文件
FileOutputStreamos=newFileOutputStream("
D:
+newDate().getTime()+files[i].getOriginalFilename());
//拿到上传文件的输入流
InputStreamin=(InputStream)files[i].getInputStream();
//以写字节的方式写文件
intb=0;
while((b=in.read())!
=-1){
os.write(b);
os.flush();
os.close();
in.close();
}catch(Exceptione){
e.printStackTrace();
上传出错"
);
intfinaltime=(int)System.currentTimeMillis();
System.out.println(finaltime-pre);
return"
/login"
/upload2"
publicStringupload2(HttpServletRequestrequest,HttpServletResponseresponse)throwsIllegalStateException,IOException{
//记录上传过