详解SpringMVC使用MultipartFile实现文件的上传

如果需要实现跨服务器上传文件,就是将我们本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的。我们需要先懂得如何配置springMVC,然后在配置ftp,最后再结合MultipartFile上传文件。

springMVC上传需要几个关键jar包,spring以及关联包可以自己配置,这里主要说明关键的jar包

1:spring-web-3.2.9.RELEASE.jar (spring的关键jar包,版本可以自己选择)

2:commons-io-2.2.jar (项目中用来处理IO的一些工具类包)

配置文件

SpringMVC是用MultipartFile来进行文件上传的,因此我们先要配置MultipartResolver,用于处理表单中的file

<!-- 上传文件解释器 -->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" />
    <property name="maxUploadSize" value="10485760" />
    <property name="maxInMemorySize" value="4096" />
    <property name="resolveLazily" value="true" />
  </bean> 

其中属性详解:

defaultEncoding配置请求的编码格式,默认为iso-8859-1

maxUploadSize配置文件的最大单位,单位为字节

maxInMemorySize配置上传文件的缓存 ,单位为字节

resolveLazily属性启用是为了推迟文件解析,以便在UploadAction 中捕获文件大小异常

页面配置

在页面的form中加上enctype="multipart/form-data"

<form id="" name="" method="post" action="" enctype="multipart/form-data">  

表单标签中设置enctype="multipart/form-data"来确保匿名上载文件的正确编码。

是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作。enctype="multipart/form-data"是上传二进制数据。form里面的input的值以2进制的方式传过去,所以request就得不到值了。

编写上传控制类

编写一个上传方法,这里没有返回结果,需要跳转页面或者返回其他值可以将void改为String、Map<String,Object>等值,再return返回结果。

    /**
 * 上传
 * @param request
 * @return
 */
@ResponseBody
@RequestMapping(value = "/upload", method = {RequestMethod.GET, RequestMethod.POST})
public void upload(HttpServletRequest request) {
  MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request;
  MultipartFile file = multipartRequest.getFile("file");//file是页面input的name名
  String basePath = "文件路径"
  try {
    MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
      if (resolver.isMultipart(request)) {
      String fileStoredPath = "文件夹路径";
      //随机生成文件名
      String randomName = StringUtil.getRandomFileName();
      String uploadFileName = file.getOriginalFilename();
      if (StringUtils.isNotBlank(uploadFileName)) {
        //截取文件格式名
        String suffix = uploadFileName.substring(uploadFileName.indexOf("."));
        //重新拼装文件名
        String newFileName = randomName + suffix;
        String savePath = basePath + "/" + newFileName;
        File saveFile = new File(savePath);
        File parentFile = saveFile.getParentFile();
        if (saveFile.exists()) {
          saveFile.delete();
        } else {
          if (!parentFile.exists()) {
            parentFile.mkdirs();
          }
        }
        //复制文件到指定路径
        FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
        //上传文件到服务器
        FTPClientUtil.upload(saveFile, fileStoredPath);
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

FTP客户端上传工具

package com.yuanding.common.util; 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; 

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 

/**
 * FTP客户端工具
 */
public class FTPClientUtil { 

  /**
   * 日志
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(FTPClientUtil.class); 

  /**
   * FTP server configuration--IP key,value is type of String
   */
  public static final String SERVER_IP = "SERVER_IP"; 

  /**
   * FTP server configuration--Port key,value is type of Integer
   */
  public static final String SERVER_PORT = "SERVER_PORT"; 

  /**
   * FTP server configuration--ANONYMOUS Log in key, value is type of Boolean
   */
  public static final String IS_ANONYMOUS = "IS_ANONYMOUS"; 

  /**
   * user name of anonymous log in
   */
  public static final String ANONYMOUS_USER_NAME = "anonymous"; 

  /**
   * password of anonymous log in
   */
  public static final String ANONYMOUS_PASSWORD = ""; 

  /**
   * FTP server configuration--log in user name, value is type of String
   */
  public static final String USER_NAME = "USER_NAME"; 

  /**
   * FTP server configuration--log in password, value is type of String
   */
  public static final String PASSWORD = "PASSWORD"; 

  /**
   * FTP server configuration--PASV key, value is type of Boolean
   */
  public static final String IS_PASV = "IS_PASV"; 

  /**
   * FTP server configuration--working directory key, value is type of String While logging in, the current directory
   * is the user's home directory, the workingDirectory must be set based on it. Besides, the workingDirectory must
   * exist, it can not be created automatically. If not exist, file will be uploaded in the user's home directory. If
   * not assigned, "/" is used.
   */
  public static final String WORKING_DIRECTORY = "WORKING_DIRECTORY"; 

  public static Map<String, Object> serverCfg = new HashMap<String, Object>(); 

  static Properties prop; 

  static{
    LOGGER.info("开始加载ftp.properties文件!");
    prop = new Properties();
    try {
      InputStream fps = FTPClientUtil.class.getResourceAsStream("/ftp.properties");
      prop.load(fps);
      fps.close();
    } catch (Exception e) {
      LOGGER.error("读取ftp.properties文件异常!",e);
    }
    serverCfg.put(FTPClientUtil.SERVER_IP, values("SERVER_IP"));
    serverCfg.put(FTPClientUtil.SERVER_PORT, Integer.parseInt(values("SERVER_PORT")));
    serverCfg.put(FTPClientUtil.USER_NAME, values("USER_NAME"));
    serverCfg.put(FTPClientUtil.PASSWORD, values("PASSWORD"));
    LOGGER.info(String.valueOf(serverCfg));
  } 

  /**
   * Upload a file to FTP server.
   *
   * @param serverCfg : FTP server configuration
   * @param filePathToUpload : path of the file to upload
   * @param fileStoredName : the name to give the remote stored file, null, "" and other blank word will be replaced
   *      by the file name to upload
   * @throws IOException
   * @throws SocketException
   */
  public static final void upload(Map<String, Object> serverCfg, String filePathToUpload, String fileStoredName)
      throws SocketException, IOException {
    upload(serverCfg, new File(filePathToUpload), fileStoredName);
  } 

  /**
   * Upload a file to FTP server.
   *
   * @param serverCfg : FTP server configuration
   * @param fileToUpload : file to upload
   * @param fileStoredName : the name to give the remote stored file, null, "" and other blank word will be replaced
   *      by the file name to upload
   * @throws IOException
   * @throws SocketException
   */
  public static final void upload(Map<String, Object> serverCfg, File fileToUpload, String fileStoredName)
      throws SocketException, IOException {
    if (!fileToUpload.exists()) {
      throw new IllegalArgumentException("File to upload does not exists:" + fileToUpload.getAbsolutePath 

());
    }
    if (!fileToUpload.isFile()) {
      throw new IllegalArgumentException("File to upload is not a file:" + fileToUpload.getAbsolutePath());
    }
    if (StringUtils.isBlank((String) serverCfg.get(SERVER_IP))) {
      throw new IllegalArgumentException("SERVER_IP must be contained in the FTP server configuration.");
    }
    transferFile(true, serverCfg, fileToUpload, fileStoredName, null, null);
  } 

  /**
   * Download a file from FTP server
   *
   * @param serverCfg : FTP server configuration
   * @param fileNameToDownload : file name to be downloaded
   * @param fileStoredPath : stored path of the downloaded file in local
   * @throws SocketException
   * @throws IOException
   */
  public static final void download(Map<String, Object> serverCfg, String fileNameToDownload, String fileStoredPath)
      throws SocketException, IOException {
    if (StringUtils.isBlank(fileNameToDownload)) {
      throw new IllegalArgumentException("File name to be downloaded can not be blank.");
    }
    if (StringUtils.isBlank(fileStoredPath)) {
      throw new IllegalArgumentException("Stored path of the downloaded file in local can not be blank.");
    }
    if (StringUtils.isBlank((String) serverCfg.get(SERVER_IP))) {
      throw new IllegalArgumentException("SERVER_IP must be contained in the FTP server configuration.");
    }
    transferFile(false, serverCfg, null, null, fileNameToDownload, fileStoredPath);
  } 

  private static final void transferFile(boolean isUpload, Map<String, Object> serverCfg, File fileToUpload,
      String serverFileStoredName, String fileNameToDownload, String localFileStoredPath) throws  

SocketException,
      IOException {
    String host = (String) serverCfg.get(SERVER_IP);
    Integer port = (Integer) serverCfg.get(SERVER_PORT);
    Boolean isAnonymous = (Boolean) serverCfg.get(IS_ANONYMOUS);
    String username = (String) serverCfg.get(USER_NAME);
    String password = (String) serverCfg.get(PASSWORD);
    Boolean isPASV = (Boolean) serverCfg.get(IS_PASV);
    String workingDirectory = (String) serverCfg.get(WORKING_DIRECTORY);
    FTPClient ftpClient = new FTPClient();
    InputStream fileIn = null;
    OutputStream fileOut = null;
    try {
      if (port == null) {
        LOGGER.debug("Connect to FTP server on " + host + ":" + FTP.DEFAULT_PORT);
        ftpClient.connect(host);
      } else {
        LOGGER.debug("Connect to FTP server on " + host + ":" + port);
        ftpClient.connect(host, port);
      }
      int reply = ftpClient.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        LOGGER.error("FTP server refuses connection");
        return;
      } 

      if (isAnonymous != null && isAnonymous) {
        username = ANONYMOUS_USER_NAME;
        password = ANONYMOUS_PASSWORD;
      }
      LOGGER.debug("Log in FTP server with username = " + username + ", password = " + password);
      if (!ftpClient.login(username, password)) {
        LOGGER.error("Fail to log in FTP server with username = " + username + ", password = " +  

password);
        ftpClient.logout();
        return;
      } 

      // Here we will use the BINARY mode as the transfer file type,
      // ASCII mode is not supportted.
      LOGGER.debug("Set type of the file, which is to upload, to BINARY.");
      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

      if (isPASV != null && isPASV) {
        LOGGER.debug("Use the PASV mode to transfer file.");
        ftpClient.enterLocalPassiveMode();
      } else {
        LOGGER.debug("Use the ACTIVE mode to transfer file.");
        ftpClient.enterLocalActiveMode();
      } 

      if (StringUtils.isBlank(workingDirectory)) {
        workingDirectory = "/";
      } 

      LOGGER.debug("Change current working directory to " + workingDirectory);
      changeWorkingDirectory(ftpClient,workingDirectory); 

      if (isUpload) { // upload
        if (StringUtils.isBlank(serverFileStoredName)) {
          serverFileStoredName = fileToUpload.getName();
        }
        fileIn = new FileInputStream(fileToUpload);
        LOGGER.debug("Upload file : " + fileToUpload.getAbsolutePath() + " to FTP server with name : "
            + serverFileStoredName);
        if (!ftpClient.storeFile(serverFileStoredName, fileIn)) {
          LOGGER.error("Fail to upload file, " + ftpClient.getReplyString());
        } else {
          LOGGER.debug("Success to upload file.");
        }
      } else { // download
        // make sure the file directory exists
        File fileStored = new File(localFileStoredPath);
        if (!fileStored.getParentFile().exists()) {
          fileStored.getParentFile().mkdirs();
        }
        fileOut = new FileOutputStream(fileStored);
        LOGGER.debug("Download file : " + fileNameToDownload + " from FTP server to local : "
            + localFileStoredPath);
        if (!ftpClient.retrieveFile(fileNameToDownload, fileOut)) {
          LOGGER.error("Fail to download file, " + ftpClient.getReplyString());
        } else {
          LOGGER.debug("Success to download file.");
        }
      } 

      ftpClient.noop(); 

      ftpClient.logout(); 

    } finally {
      if (ftpClient.isConnected()) {
        try {
          ftpClient.disconnect();
        } catch (IOException f) {
        }
      }
      if (fileIn != null) {
        try {
          fileIn.close();
        } catch (IOException e) {
        }
      }
      if (fileOut != null) {
        try {
          fileOut.close();
        } catch (IOException e) {
        }
      }
    }
  } 

  private static final boolean changeWorkingDirectory(FTPClient ftpClient, String workingDirectory) throws IOException{
    if(!ftpClient.changeWorkingDirectory(workingDirectory)){
      String [] paths = workingDirectory.split("/");
      for(int i=0 ;i<paths.length ;i++){
        if(!"".equals(paths[i])){
          if(!ftpClient.changeWorkingDirectory(paths[i])){
            ftpClient.makeDirectory(paths[i]);
            ftpClient.changeWorkingDirectory(paths[i]);
          }
        }
      }
    }
    return true;
  } 

  public static final void upload(Map<String, Object> serverCfg, String filePathToUpload, String fileStoredPath, String  

fileStoredName)
      throws SocketException, IOException {
    upload(serverCfg, new File(filePathToUpload), fileStoredPath, fileStoredName);
  } 

  public static final void upload(Map<String, Object> serverCfg, File fileToUpload, String fileStoredPath, String  

fileStoredName)
      throws SocketException, IOException {
    if(fileStoredPath!=null && !"".equals(fileStoredPath)){
      serverCfg.put(WORKING_DIRECTORY, fileStoredPath);
    }
    upload(serverCfg, fileToUpload, fileStoredName);
  } 

  public static final void upload(String filePathToUpload, String fileStoredPath)throws SocketException, IOException {
    upload(serverCfg, filePathToUpload, fileStoredPath, "");
  } 

  public static final void upload(File fileToUpload, String fileStoredPath)throws SocketException, IOException {
    upload(serverCfg, fileToUpload, fileStoredPath, "");
  } 

  public static String values(String key) {
    String value = prop.getProperty(key);
    if (value != null) {
      return value;
    } else {
      return null;
    }
  } 

}

ftp.properties

#服务器地址
SERVER_IP=192.168.1.1 

#服务器端口
SERVER_PORT=21 

#帐号名
USER_NAME=userftp 

#密码
#PASSWORD=passwordftp 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • springMVC结合AjaxForm上传文件

    最近在项目中需要上传文件文件,之前一直都是form提交的,尝试了一下AjaxForm,感觉还比较好用,写篇随笔mark下,供以后使用. 准备工作: 下载jquery-form.js 相关jar: commons-fileupload-1.1.1.jar commons-io-1.3.2.jar 在spring-servlet.xml进行multipartResolver配置: <bean id="multipartResolver" class="org.springf

  • Spring MVC中上传文件实例

    SpringMVC(注解)上传文件需要注意的几个地方: 1.form的enctype="multipart/form-data",这个是上传文件必须的 2.applicationContext.xml配置: 复制代码 代码如下: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.w

  • springMVC配置环境实现文件上传和下载

    最近的项目中用到了文件的上传和下载功能,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试. 下面就是springMVC配置环境实现文件上传和下载的具体步骤,供大家参考,具体内容如下 一. 基础配置: maven导包及配置pom.xml,导包时除开springmvc的基础依赖外,需要导入文件上传下载时用到的commons-io.jsr和commons-fileupload.jar: <project xmlns="http://maven.apache.org/POM/4.0.0&

  • Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!--配置下载返回类型--> <bean class="or

  • MyBatis与SpringMVC相结合实现文件上传、下载功能

    环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何使用input上传文件到服务器指定目录,或保存到数据库中:如何从数据库下载文件,和显示图像文件并实现缩放. 将文件存储在数据库中,一般是存文件的byte数组,对应的数据库数据类型为blob. 首先要创建数据库,此处使用MySql数据库. 注意:文中给出的代码多为节选重要片段,并不齐全. 1. 前期准备 使用maven创建一个springMVC+spring+mybatis+mysql的项目

  • springMVC实现前台带进度条文件上传的示例代码

    项目框架采用spring+hibernate+springMVC如果上传文件不想使用flash那么你可以采用HTML5;截图前段模块是bootstarp框架;不废话直接来代码;spring-mvc配置文件;效果截图如下: 详细实现如下: 1.mvc-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/s

  • SpringMVC文件上传的配置实例详解

    记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload-1.1.1.jar commons-io-1.3.2.jar 然后在spring-servlet.xml进行multipartResolver配置,不配置好上传会不好用. <bean id="multipartResolver" class="org.springfram

  • Spring MVC环境中文件上传功能的实现方法详解

    前言 我们在实际开发过程中,尤其是web项目开发,文件上传和下载的需求的功能非常场景,比如说用户头像.商品图片.邮件附件等等.其实文件上传下载的本质都是通过流的形式进行读写操作,而在开发中不同的框架都会对文件上传和下载有或多或少的封装,这里就以Spring MVC环境中文件的上传为例,讲解Spirng MVC环境下的文件上传功能实现.下面话不多说了,来一起看看详细的介绍吧. 一.客户端编程 由于多数文件上传都是通过表单形式提交给后台服务器的,因此,要实现文件上传功能,就需要提供一个文件上传的表单

  • SpringMVC文件上传 多文件上传实例

    必须明确告诉DispatcherServlet如何处理MultipartRequest.SpringMVC中提供了文件上传使用方式如下配置xxx-servlet.xml,添加如下代码: 复制代码 代码如下: <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <!-- 设置

  • SpringMVC 文件上传配置,多文件上传,使用的MultipartFile的实例

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下如何使用SpringMVC进行表单上的文件上传以及多个文件同时上传的步骤 文件上传项目的源码下载地址:demo 一.配置文件: SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->

随机推荐