1.创建web项目fileDemo
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>fileDemo</display-name>
<!-- 统一编码 -->
<filter>
<filter-name>charsetEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 应用上下文配置文件 -->
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载/WEB-INF/[servlet-name]-servlet.xml -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>2.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 扫描路径 -->
<context:component-scan base-package="com.fileDemo.file" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置根视图 -->
<mvc:view-controller path="/" view-name="index"/>
<!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,
@DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 -->
<mvc:annotation-driven />
<!-- 静态资源配置 -->
<mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources>
<!-- 视图层配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/web/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans>3.FileController.java
package com.fileDemo.file;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping(value="/file")
public class FileController {
@RequestMapping(value="/listFile",method=RequestMethod.GET)
public String hello(Model model){
model.addAttribute("msg", "文件上传");
return "index";
}
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
public String uploadFile(Model model,HttpServletRequest request,@RequestParam(value = "file", required = false) MultipartFile file){
String path = request.getServletContext().getRealPath("WEB-INF/upload");
File localFile = null;
if(file != null){
localFile = new File(path+"/"+file.getOriginalFilename());
this.copyFile(file, localFile);
System.out.println(localFile.getName());
}
model.addAttribute("msg", "文件上传成功");
return "index";
}
private static boolean copyFile(MultipartFile src,File f){
boolean flag = false;
InputStream in = null;
OutputStream out = null;
try{
in = src.getInputStream();
out = new FileOutputStream(f);
byte[] buff =new byte[1024];
int len=0;
while((len=in.read(buff))>0){
out.write(buff, 0, len);
}
out.close();
in.close();
flag = true;
}catch(Exception e){
}
return flag;
}
}
4.index.jsp
<%@ 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>Java 上传文件</title>
</head>
<%
%>
<body>
${msg }
<form enctype="multipart/form-data" id="_importForm" method="post" action="${pageContext.request.contextPath}/file/uploadFile">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
5.项目结构如图(poi的jar不需要)

本文介绍了一个使用Spring MVC框架实现文件上传的例子。通过配置web.xml和springmvc-servlet.xml,定义FileController处理文件上传请求,并展示如何在前端页面实现文件选择及提交。
1243

被折叠的 条评论
为什么被折叠?



