spring springmvc實現檔案上傳
阿新 • • 發佈:2019-01-07
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
3.FileController.java<?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>
4.index.jsppackage 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; } }
<%@ 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不需要)