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
  • 相关阅读:
    Java实体书写规范
    Mybatis配置中遇到的问题和问题分析
    Ubuntu下eclipse的Extjs提示插件安装
    Ubuntu中root用户和user用户的相互切换
    ubuntu下启动和关闭tomcat的简单方法
    Ubuntu上安装Maven Eclipse以及配置
    IE8兼容<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    心情
    更改Eclipse下Tomcat的部署目录
    Tomcat:IOException while loading persisted sessions: java.io.EOFException 解决
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/8920933.html
Copyright © 2011-2022 走看看