09-spring mvc 文件上传

spring mvc文件上传

spring mvc的3种文件上传方式:https://www.cnblogs.com/fjsnail/p/3491033.html

1、导入jar包
在这里插入图片描述
在maven项目中导入的依赖

	<!-- 文件上传所依赖的jar包 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.2</version>
		</dependency>

2、在spring mvc的配置文件(springMVC-servlet.xml)中进行配置

配置一个Mutipart Resolver来告诉DispatchServlet如何解析一个mutipart 请求

 <!-- 配置支持文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="500000"/>

3、实现文件上传的controller层(FileUploadController)

package com.zm.upload.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/*文件上传*/
@Controller
public class FileUploadController {

    @RequestMapping("/load")
    public String fileForm(){
        return "file";
    }
    @RequestMapping("/uploadFile")
    public String save( MultipartFile file, Model model, HttpServletRequest req) throws IOException {
        System.out.println(file);
        System.out.println(file.getOriginalFilename());
    
         if(file!=null&&file.getOriginalFilename()!=null&&file.getOriginalFilename().length()>0) {
          /*  //文件上传到的目录
            String uploadPath = req.getSession().getServletContext().getRealPath("img");
            System.out.println(uploadPath);*/
            String uploadPath = "E:/upload";
            File mk = new File(uploadPath);
            if(!mk.exists()&&!mk.isDirectory()){
                System.out.println("该目录不存在,创建目录");
                mk.mkdir();
            }
            //获取要上传的文件
            String origName = file.getOriginalFilename();
            //获取要上传的文件的后缀名
            String extendsName = origName.substring(origName.lastIndexOf("."));
            System.out.println("后缀名:"+extendsName);
            //上传后到的文件名
            String newFileName = UUID.randomUUID().toString() + extendsName;
            System.out.println("上传后的文件名:"+newFileName);
            //上传后的文件地址
            String newFilePath = uploadPath+"/"+newFileName;
            System.out.println(newFilePath);
            File uploadFile = new File(uploadPath,newFileName);
            //上传文件
            file.transferTo(uploadFile);
            model.addAttribute("filePath","/file/"+newFileName);
        }
        return "success";
    }
}

4、实现文件上传的web页面(file.jsp)

<form action="uploadFile" method="post" enctype="multipart/form-data">
   上传的文件:<input type="file" name="file"><br/>
    <input type="submit" value="上传">
</form>

5、tomcat、idea和spring mvc实现上传文件与项目路径分离

在tomcat中的配置
 在tomcat安装目录下的conf–>server.xml文件的<host></host>标签内进行配置:

 <!--配置上传图片的虚拟路径。path中是虚拟路径,doBase中是实际路径,当访问../file
 时,实际访问的是E:\\upload下的文件  -->
 <Context path="/file" docBase="E:\\upload" />

在idea中进行设置
依次点击run—>edit configurations,进入如下页面:
在这里插入图片描述
点击Deployment进入如下页面:
在这里插入图片描述
点击如图所示的(+),配置上传图片的虚拟路径。

当上传图片的操作完成后,就可以通过虚拟路径访问非项目中的图片资源了。

注意:在web页面中,要想实现图片上传,必须在<form></form>中添加属性。

 enctype="multipart/form-data"

它会告诉浏览器我们提交的是一个Mutipart请求而不是一个普通的form请求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值