SPringMVC的文件上传与下载

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值