SPringMVC的文件上传与下载

来源:互联网 发布:淘宝商家进货渠道 编辑:程序博客网 时间:2024/05/17 07:12

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>

原创粉丝点击