(七)SpringBoot图片上传

项目目录

只需要这些标红的部分,就可以实现图片上传。

 一、编辑application.properties 文件

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb

设置图片上传大小 

二、创建 UploadController 

package com.springboot.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {
    // 因为uploadPage.jsp 在WEB-INF下,不能直接从浏览器访问,所以要在这里加一个uploadPage跳转,这样就可以通过
    @RequestMapping("/uploadPage")
    public String uploadPage() {
        return "uploadPage";   //过度跳转页
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)//  等价于@PostMapping("/upload") 
    public String uplaod(HttpServletRequest req, @RequestParam("file") MultipartFile file, Model m) {
    	//1. 接受上传的文件  @RequestParam("file") MultipartFile file
        try {
            //2.根据时间戳创建新的文件名,这样即便是第二次上传相同名称的文件,也不会把第一次的文件覆盖了
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            //3.通过req.getServletContext().getRealPath("") 获取当前项目的真实路径,然后拼接前面的文件名
            String destFileName = req.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
            //4.第一次运行的时候,这个文件所在的目录往往是不存在的,这里需要创建一下目录(创建到了webapp下uploaded文件夹下)
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            //5.把浏览器上传的文件复制到希望的位置
            file.transferTo(destFile);
            //6.把文件名放在model里,以便后续显示用
            m.addAttribute("fileName", fileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        }

        return "showImg";
    }
}

关键代码说明:

 (1)@RequestParam("file") 

接收上传参数,这里的 参数"file"是指前端上传文件名  ——指的是  uploadPage.jsp选择图片:<input type="file" name="file" accept="image/*" /> <br>  neme="file"

  (2)file.transferTo(destFile)

把上传的文件file 复制到 descFile下,并且命名为 fileName

三、创建上传页面 uploadPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片上传</title>
</head>
<body>
    <form action="upload" method="post" enctype="multipart/form-data">
        选择图片:<input type="file" name="file" accept="image/*" /> <br>
        <input type="submit" value="立刻上传">
    </form>
</body>
</html>

四、创建展示上传图片页面  showImg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传图片</title>
</head>
<body>
    <img src="/uploaded/${fileName}">
</body>
</html>

五 、启动类

/**
 * 
 */
package com.springboot.application;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


//

//@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan("com.springboot.controller,com.springboot.config")
@MapperScan("com.springboot.mapper")
public class Application {
	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);

	}
}

六、效果展示

http://127.0.0.1:8080/uploadPage

猜你喜欢

转载自blog.csdn.net/qq_38214630/article/details/86506671
今日推荐