(十)springmvc之文件的处理

一、同步上传文件

  • 导入common-fileupload这个jar包。

  • 配置     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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


 <!-- 激活@Required @Autowired @Resource等标注-->  
    <context:annotation-config></context:annotation-config>  
    
      <!-- DispatcherServlet上下文,扫描base-package包中的类,并自动加载到spring容器中 -->  
    <context:component-scan base-package="com.shyroke.controller">
    </context:component-scan>  
   
  <!--   启用@Component,@Controller,@Service,@Repository注解驱动 -->  
      <mvc:annotation-driven/>    
  
     <mvc:default-servlet-handler />
     
     <!-- 文件处理 -->
   <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10485760000"></property>
        <property name="maxInMemorySize" value="40960"></property>
    </bean>
     
    <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/views/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean> 
</beans>
  •  index.jsp
<body>

    <form method="post" id="file_form1" action="<%=request.getContextPath()%>/uploadController/uploadForm" 
          enctype="multipart/form-data">
        <input type="text" name="dogid" id="dogid"     value="用户名"/>
        <input type="text" name="dogname" id="dogname" value="复选框1"/>
        <input type="file" name="faceimage" id="faceimage" size="40"/>
        <input type="submit" value="上传文件"/>
    </form>    
</body>
  •  controller
package com.shyroke.controller;

import java.io.File;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.shyroke.bean.DogBean;

@Controller
@RequestMapping(value="/uploadController")
public class UploadController {

    @Autowired
    private ServletContext context;

    @RequestMapping(value = "/uploadForm")
    public String uploadFile(
            DogBean dogBean,
            @RequestParam(name = "faceimage", required = false) MultipartFile faceimage)
            throws Exception {

        System.out.println(dogBean);

        System.out.println(faceimage);

        String dogid = dogBean.getDogid();
        String dogname = dogBean.getDogname();

        String name = faceimage.getName();
        String filename = faceimage.getOriginalFilename();
        InputStream inputStream = faceimage.getInputStream();
        /**
         * 输出控件的名称,faceimage
         */
        System.out.println("name = " + name);
        /**
         * 文件的名称
         */
        System.out.println("filename = " + filename);
        /**
         * 获取文件流
         */
        System.out.println("inputStream = " + inputStream);

        String uploadDir = context.getRealPath("/uploadfiles");
        System.out.println(uploadDir);

        filename = System.currentTimeMillis() + "_" + filename;
        File destFile = new File(uploadDir + "/" + filename);
        faceimage.transferTo(destFile);

        return null;
    }
}

二、异步上传文件

  • 导入common-fileupload这个jar包、配置     springmvc-servlet.xml 如上

  • index.jsp
    2:ajax来提交。
    <form method="post" id="file_form2" action="" 
          enctype="multipart/form-data">
        <input type="text" name="dogid" id="dogid"     value="用户名"/>
        <input type="text" name="dogname" id="dogname" value="复选框1"/>
        <input type="file" name="faceimage" id="faceimage" size="40"/>
        <input type="button" value="上传文件" onclick="uploadFile();"/>
    </form>
  • controller
package com.shyroke.controller;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shyroke.bean.DogBean;

@Controller
@RequestMapping(value = "/uploadController")
public class UploadController {

    @Autowired
    private ServletContext context;
    
    @RequestMapping(value = "/uploadAjax")
    @ResponseBody
    public Map<String, Object> uploadAjax(DogBean dogBean,
            @RequestParam(name = "faceimage", required = false) MultipartFile faceimage) throws Exception {

        System.out.println(dogBean);

        System.out.println(faceimage);

        String dogid = dogBean.getDogid();
        String dogname = dogBean.getDogname();

        String name = faceimage.getName();
        String filename = faceimage.getOriginalFilename();
        InputStream inputStream = faceimage.getInputStream();
        /**
         * 输出控件的名称,faceimage
         */
        System.out.println("name = " + name);
        /**
         * 文件的名称
         */
        System.out.println("filename = " + filename);
        /**
         * 获取文件流
         */
        System.out.println("inputStream = " + inputStream);

        String uploadDir = context.getRealPath("/uploadfiles");
        System.out.println(uploadDir);
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        try {
            filename = System.currentTimeMillis() + "_" + filename;
            File destFile = new File(uploadDir + "/" + filename);
            faceimage.transferTo(destFile);
            jsonMap.put("flag", true);
        } catch (Exception e) {
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", "错误");
        }
        return jsonMap;
    }
}
原文地址:https://www.cnblogs.com/shyroke/p/7778685.html