springmvc上传与下载

文件上传

结构图

springmvc上传与下载_第1张图片

导入依赖


      jstl
      jstl
      1.2
    
    
      org.springframework
      spring-webmvc
      5.2.8.RELEASE
    
    
      commons-fileupload
      commons-fileupload
      1.4
    
    
      junit
      junit
      4.12
      test
    
    
      javax.servlet
      javax.servlet-api
      3.0.1
    

web.xml配置



  Archetype Created Web Application

  
    dispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring.xml
    
    
    1
  
  
    dispatcherServlet
    /
  

  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
    
      forceEncoding
      true
    
  
  
    encodingFilter
    /*
  


上传一个或多个文件

index.jsp代码

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


上传文件:

多文件

上传文件1: 上传文件2: 上传文件3:

WEB-INF下的success.jsp代码

springmvc上传与下载_第2张图片

spring.xml




    

    
        
        
    

    

    

    
        
        
    

FileController代码

运行的时候,浏览器页面要在WEB-INF下的index.jsp,跑完该项目时,页面是webapp下的index.jsp,所以跑完之后切换请求路径(/toFile)进入WEB-INF下的index.jsp

package com.tmg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

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

@Controller
public class FileController {
    private static final String DIR = "D:\\picture\\";

    @RequestMapping("/toFile")
    public String text() {

        return "/index";
    }

    @RequestMapping("file")
    public String text1(MultipartFile picture) throws IOException {
        String filename = picture.getOriginalFilename();
        picture.transferTo(new File(DIR, filename));
        return "success";
    }

    @RequestMapping("/files")
    public String text2(MultipartFile[] pictures) {

        try {
            for (MultipartFile picture : pictures) {
                String filename = picture.getOriginalFilename();

                if("".equals(filename)){
                    continue;
                }
                picture.transferTo(new File(DIR, filename));
                System.out.println(filename);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return "success";
        }
    }

}

文件下载

DownloadController代码

跑的时候记得在路径带上 file=XXX ,并在执行前指定自己下载目录

package com.tmg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * 下载控制器
 */
@Controller
public class DownloadController {

    @RequestMapping("/download")
    public void download(String file,  HttpServletResponse response) throws IOException {
        //下载文件的路径
        String path = "D:\\picture\\";
        File downFile = new File(path+file);
        if(downFile.exists()){
            //设置浏览器下载内容类型,响应头
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition","attachment;filename="+file);
            //通过流发送文件
            Files.copy(downFile.toPath(),response.getOutputStream());
        }
    }
}

你可能感兴趣的:(springmvc,java,java)