zoukankan      html  css  js  c++  java
  • Spring Boot—04文件上传

    package com.smartmap.sample.ch1.controller.view;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    import com.smartmap.sample.ch1.service.UserService;
    
    @Controller
    @RequestMapping("/system/page/user")
    public class UserViewController {
        @Autowired
        UserService userService;
    
        @Autowired
        private Environment environment;
    
        @GetMapping("/list.html")
        public String userList(Model model) {
            String osName = System.getProperty("os.name");
            model.addAttribute("name", "hello world");
            model.addAttribute("host", osName);
            return "user/list";
        }
    
        @GetMapping("/userdetail.html")
        public String userdetail(String id) {
            return "admin/userInfo.jsp";
        }
    
        /**
         * 文件上传
         * 
         * curl -XPOST 'http://127.0.0.1:8080/system/page/user/upload.html' -F
         * "multipartFile=@/D/Project/JavaWeb/SpringBoot/01HelloSpringBoot/readme.txt"
         * -F "name=123456789"
         * 
         * curl -XPOST 'http://127.0.0.1:8080/system/page/user/upload.html' -F
         * "multipartFile=@readme.txt;type=application/octet-stream" -F "name=123456789"
         * 
         * @param name
         * @param multipartFile
         * @return
         * @throws IllegalStateException
         * @throws IOException
         */
        @PostMapping("/upload.html")
        @ResponseBody
        public String handleFormUpload(String name, MultipartFile multipartFile) throws IllegalStateException, IOException {
            String resultMessage = "{success: false, message: 'upload fail'}";
            System.out.println(multipartFile);
            if (!multipartFile.isEmpty()) {
                String fileName = multipartFile.getOriginalFilename();
                // InputStream is = multipartFile.getInputStream();
                String fileSavePath = environment.getProperty("file.upload.path", "");
                if (!fileSavePath.equals("")) {
                    File file = new File(fileSavePath + java.io.File.separator + fileName);
                    if (file.exists()) {
                        file.delete();
                    }
                    multipartFile.transferTo(file);
                    resultMessage = "{success: true, message: 'upload success'}";
                }
            }
            return resultMessage;
        }
    
    }



    application.properties

    file.upload.path=D:/Project/JavaWeb/SpringBoot/01HelloSpringBoot/fileUpLoad
    
    spring.servlet.multipart.enabled=true
    spring.servlet.multipart.file-size-threshold=0
    spring.servlet.multipart.location=D:/Project/JavaWeb/SpringBoot/01HelloSpringBoot/temp
    spring.servlet.multipart.max-file-size=10MB
    spring.servlet.multipart.max-request-size=10MB
    spring.servlet.multipart.resolve-lazily=false
  • 相关阅读:
    STL中的stack(堆栈)
    单链表的创建与删除
    面试题四 从尾到头打印链表
    第 1 章 第 2 题 空间敏感排序问题 位向量实现( bitset位向量 )
    第 1 章 第 2 题 位向量的数组实现问题 位运算实现
    第 1 章 第 1 题 高级语言的排序问题 C++标准算法实现
    面试题三 替换空格
    面试题二 二维数组中的查找
    面试题一 赋值运算符函数
    不为客户连接创建子进程的并发回射服务器( poll实现 )
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/8920933.html
Copyright © 2011-2022 走看看