用SpringMVC实现的上传下载

1、导入相关jar包

commons-fileupload.jar

commons-io.jar

2、配置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>SpringMVC_fileUpLoad</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  	<filter>
		<filter-name>encodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>	
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	
</web-app>

  

3、编写上传页面

<%@ 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="upload.action" method="post"enctype="multipart/form-data">
	<input type="file" name="pic" />
	<input type="submit" value="上传" />
</form>
</body>
</html>

4、配置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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	<context:component-scan base-package="com.etc"></context:component-scan>
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="102400"/>
	</bean>
	
	<bean id="fileUpLoadController" class="com.etc.fileupload.FileUpLoadController"/>
</beans>

5、编写handler处理器

package com.etc.fileupload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

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.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUpLoadController {

	@RequestMapping("/upload.action")
	public String UpLoad(@RequestParam MultipartFile pic,Model model) throws IOException{   //上传
		if(pic!=null||!pic.isEmpty()){
			byte[] b = pic.getBytes();
//			System.out.println(pic.getOriginalFilename());  //123.jpg
//			System.out.println(pic.getName());              //pic
//			System.out.println(pic.getContentType());       //image/jpeg
			
			FileOutputStream out=new FileOutputStream("E:"+File.separator+"images"+File.separator+pic.getOriginalFilename());
			out.write(b);
			model.addAttribute("filename",pic.getOriginalFilename());
		} 
		return "uploadsus.jsp";		
	}
	
	@RequestMapping("/down.action")
	public ResponseEntity<byte[]> downFile(HttpServletRequest r,String filename) throws IOException{   //下载
		//配置http请求头文件信息
		
		HttpHeaders headers = new HttpHeaders(); 
		String path=r.getServletContext().getRealPath("/img")+ File.separator + filename;
		
		File file=new File(path);
		     
	    headers.setContentDispositionFormData("attachment",filename);//不自动打开   
	    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   //头文件内容类型
	    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
	                                       headers, HttpStatus.CREATED);   
	}
}

6、编写下载页面

<%@ 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>
	<h1>图片上传成功</h1>
	<img src="img/${filename}" alt="图片" width="200px"/>
	
	<br/><a href="down.action?filename=${filename}">下载</a>
</body>
</html>

  

  

  

  

原文地址:https://www.cnblogs.com/1020182600HENG/p/6906798.html