springmvc upload pictures to the Tomcat virtual directory

I. Introduction

By uploading files to the virtual directory tomcat achieve separate code and resource files.

Second, the environment

spring+springmvc+mybatis

Third, code implementation

1. Import file upload jar

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4 </version>
        </dependency>

 

2. Set the multipart type parser springmvc.xml

<! - when SpringMVC upload files, you need to configure MultipartResolver processor -> 
    < bean the above mentioned id = "the MultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > 
        < Property name = "defaultEncoding" value = " 8-UTF " /> 
        <-! specify the upload file size can not exceed the total 60000K. Note maxUploadSize attributes for a single file is not a limitation, but the capacity of all files and      -> 
        < Property name = "maxUploadSize" value = "600000000" /> 
        < Property name = "maxInMemorySize" value = "
    >

 

3. Create a virtual directory under the tomcat

  • Create a directory called FileDir in the root directory of tomcat (Of course, this directory can also be established in other places)

 

  •  In the tomcat's conf / server.xml, configure the virtual directory. Add the following line
<Context path="/FileDir" docBase="D:\tomcats\Tomcat7-8080-idea\FileDir"/>

 

 

  • 1.jpg add a picture to the virtual directory, and start the tomcat test. Visit: http: // localhost: 8080 / FileDir / 1.jpg. Can request the picture, the configuration was successful.

4.springmvc upload pictures to the virtual directory

  • jsp page
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>测试页</title>
</head>
<body>
    <h2>测试文件上传</h2>
    <form action="upload" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="realname"><br>
        头像:<input type="file" name="picFile"><br>
        <input type="submit" value="上传">
    </form>
    <c:if test="${pic != null}">
        <img src="/FileDir/${pic}" >
    </c:if>
</body>
</html>

 

  • controller
@RequestMapping(value = "/upload",method = {RequestMethod.POST})
    public String upload(MultipartFile picFile,String realname,Model model) throws Exception{
        if(!picFile.isEmpty()){
            System.out.println("realname:"+realname);
            //存储图片的路径
            String filePath = "D:\\tomcats\\Tomcat7-8080-idea\\FileDir\\";
            String originalFilename = picFile.getOriginalFilename();
            //获取文件后缀名称
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //新图片名称
            String fileName = UUID.randomUUID().toString()+suffix;
            //上传图片
            File newFile = new File(filePath+fileName);
            picFile.transferTo(newFile);
            //返回页面
            model.addAttribute("pic",fileName);
        }
        return "test";
    }

 

Guess you like

Origin www.cnblogs.com/helf/p/11106700.html