SpringMVC 通过commons-fileupload实现文件上传功能

配置

web.xml



 
 
  springmvc
  org.springframework.web.servlet.DispatcherServlet
  
   contextConfigLocation
   classpath:applicationContext.xml
  
  1
 
 
  springmvc
  /
 

SpringMVC配置文件 applicationContext.xml

上传文件的核心配置类:CommonsMultipartResolver,注意id="multipartResolver"不要写错




 
 
 
 
 
 

 
 
  
  
  
  
 

 
 
  
  
  
  
  
 

文件上传 Controller

上传实现一

package com.pro.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {
  /*
   * 采用file.transferTo 来保存上传的文件
   */
  @RequestMapping("/upload2")
  public Map fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

    //上传路径保存设置
    String path = request.getServletContext().getRealPath("/upload");
    File realPath = new File(path);
    if (!realPath.exists()){
      realPath.mkdir();
    }
    //上传文件地址
    System.out.println("上传文件保存地址 --> "+realPath);

    //通过CommonsMultipartFile的方法直接写文件(注意这个时候)
    file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));

    Map hashMap = new HashMap<>();
    hashMap.put("code", 0);
    hashMap.put("msg", "上传成功");

    return hashMap;
  }
}

上传实现二

这里的文件名称没有使用 UUID组合名称 为了方便测试

package com.pro.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {

  // @RequestParam("file") 将 name=file 控件得到的文件封装成 CommonsMultipartFile 对象
  // 批量上传把 CommonsMultipartFile 改为数组即可
  @RequestMapping("/upload")
  public String upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
    // 获取文件名称
    String uploadFileName = file.getOriginalFilename();

    // 如果文件名为空, 直接返回首页
    if ("".equals(uploadFileName)) {
      return "file upload error";
    }

    System.out.println("上传文件名 --> " + uploadFileName);


    // 设置文件的保存位置
    String path = request.getServletContext().getRealPath("/upload");
    // 判断路径是否存在
    File realPath = new File(path);
    if (!realPath.exists()) {
      // 如果不存在就创建
      realPath.mkdir();
    }

    System.out.println("文件保存路径 --> " + realPath);

    // 获取文件输入流
    InputStream is = file.getInputStream();
    // 获取文件输出流
    FileOutputStream os = new FileOutputStream(new File(realPath, uploadFileName));

    // 缓冲区读写文件
    byte[] buffer = new byte[1024];
    int len;
    while ((len = is.read(buffer)) != -1) {
      os.write(buffer, 0, len);
      os.flush();
    }

    // 关闭流
    os.close();
    is.close();

    return "file upload success";
  }
}

测试

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

 
  $Title$
 
 
  

依赖

核心依赖就是 commons-fileupload



  
  
    junit
    junit
    4.13
  
  
  
    org.springframework
    spring-webmvc
    5.2.0.RELEASE
  
  
  
    commons-fileupload
    commons-fileupload
    1.3.3
  
  
  
    javax.servlet
    javax.servlet-api
    4.0.1
  
  
  
    javax.servlet.jsp
    jsp-api
    2.2
  
  
  
    javax.servlet
    jstl
    1.2
  

到此这篇关于SpringMVC 通过commons-fileupload实现文件上传的文章就介绍到这了,更多相关SpringMVC 实现文件上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringMVC 通过commons-fileupload实现文件上传功能)