1.導入jar包
2、配置SPringMVC.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 掃描註解 -->
<context:component-scan base-package="com.mingde"></context:component-scan>
<!-- 配置映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置適配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 響應跳轉 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/upload/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件的上傳和下載配置 -->
<!-- 注意:這裏的id名必須定義成此名,否則DispatcherServlet找不到這個處理文件上傳的bean對象 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置上傳文件的大小(以字節爲單位) -->
<property name="maxUploadSize" value="10478746"></property>
<!-- 請求的編碼格式,必須和jsp的pageEncoding屬性一致,以便確定讀取表單的內容,默認爲ISO-8859-1,但這裏我們一般會改爲UTF-8 -->
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
控制層
package com.mingde.controller;
import java.io.File;
import java.io.UnsupportedEncodingException;
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.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("updownload")
public class Contorller {
@RequestMapping("/upload")
public String upload(HttpServletRequest request,MultipartFile mFile) throws Exception{
//得到上傳文件要到達的物理位置
String realPath = request.getServletContext().getRealPath("/upload");
//得到文件名
String originalFilename = mFile.getOriginalFilename();
//構造上傳後的文件名
String fullName=realPath+File.separator+originalFilename;
System.out.println(fullName);
//保存上傳的文件到服務端的/upload目錄中
mFile.transferTo(new File(fullName));
return "filelist";
}
@RequestMapping("/down")
public ResponseEntity<byte[]> downFile(HttpServletRequest request,String filename) throws Exception{
String realPath = request.getServletContext().getRealPath("/upload");
File file=new File(realPath+File.separator+filename);
//獲取請求頭
HttpHeaders header=new HttpHeaders();
//對文件進行中午處理
String FileName=new String(filename.getBytes("UTF-8"),"ISO-8859-1" );
//爲請求頭設置其以附件下載
header.setContentDispositionFormData("attachment", FileName);
System.out.println(FileName);
//設置請求頭的內容類型(二進制流)
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), header,
HttpStatus.CREATED);
return re;
}
}
JSP頁面
在頁面進行文件上傳時,要設置表單的method=post ; enctype="multipart/form-data"
上傳頁面
<%@ 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="updownload/upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="mFile" >
<input type="submit" >
</form>
</body>
</html>
下載頁面
<%@page import="java.io.File"%>
<%@ 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>
<div>
<%
String upload=application.getRealPath("/upload");
File dirs=new File(upload);
File[] files=dirs.listFiles();
//遍歷
for(File f:files){
out.println("<a href='down.action?filename="+f.getName()+"'>"+f.getName()+"</a>");
}
%>
</div>
</body>
</html>