SSM (Spring+SpringMVC+Mybatis) framework file upload

        The file upload used in this article is based on the file upload mechanism that comes with the SSM framework. I don’t understand the principle, so I put the code here first for testing and future reference.

        Write foreground test.jsp

<%--
  Created by IntelliJ IDEA.
  User: Shan Jizhong
  Date: 2016/11/16
  Time: 11:05
  Description:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="${website}file/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file"><br><br>
        <input type="text" name="name">
        <input type="submit" value="上传">
    </form>
</body>
</html>

         Simple form, and has a field name that it tries to get

         Backend Controller

package com.pandawork.web.controller;

import com.pandawork.web.spring.AbstractController;
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 org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author ShanJZ
 * @Description
 * @time 2016/11/16 11:01
 */
@Controller
@RequestMapping("/file")
public class FileController extends AbstractController{
    @RequestMapping(value = "/to/test")
    public String toTest(){
        return "/file/test";
    }
    @RequestMapping(value = "/upload")
    public String  springUpload(HttpServletRequest request, @RequestParam("name") String name) throws IllegalStateException, IOException
    {
        //Initialize the current context to CommonsMutipartResolver (multipart resolver)
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //Check if there is enctype="multipart/form-data" in the form
        if(multipartResolver.isMultipart(request)) {
            // Turn the request into a multipart request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //Get all file names in multiRequest
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext()) {
                // loop through all files at once
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null) {
                    String path="E:/springUpload"+"/"+file.getOriginalFilename();
                    //upload
                    file.transferTo(new File(path));
                }
            }
        }
        System.out.println(name);
        return "/success";
    }
}

         Save the file to the springUpload folder under the E drive

Note: The code refers to this blog: http://www.cnblogs.com/fjsnail/p/3491033.html By the way, thanks to the blogger (hand over)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326491523&siteId=291194637