通过简单步骤实现SpringMVC文件上传

网友投稿 383 2023-07-12

通过简单步骤实现SpringMVC文件上传

通过简单步骤实现SpringMVC文件上传

这篇文章主要介绍了通过简单步骤实现SpringMVC文件上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、创建文件上传FileController类

package com.byzore.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

import java.io.File;

import java.io.IOException;

@Controller

@RequestMapping("/file")

public class FileController {

@RequestMapping("/fileUpload")

/**

* MultipartFile 选择文件

*/

public String fileupload(HttpSession session, MultipartFile file,String author)throws IOException{

System.out.println(file);

/**

* 如何处理文件

*/

if (!file.isEmpty()){

//获取文件名称

String fileName=file.getOriginalFilename();

//获取到需要上传的路径

String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");

//创建文件对象

File uploadfile=new File(realPath+"\\"+fileName);

//如何上传文件

file.transferTo(uploadfile);

}

return "index";

}

@RequestMapping("/fileUploads")

/**

* 多文件上传

*/

public String fileuploads(HttpSession session, MultipartFile[] uploadFiles,String author)throws IOException{

System.out.println(uploadFiles);

for (MultipartFile file: uploadFiles) {

/**

* 如何处理文件

*/

if (!file.isEmpty()){

//获取文件名称

String fileName=file.getOriginalFilename();

//获取到需要上传的路径

String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");

//创建文件对象

File uploadfile=new File(realPath+"\\"+fileName);

//如何上传文件

file.transferTo(uploadfile);

}

}

return "index";

}

}

二、编辑applicationContext.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">

三、创建fileUpload.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringMVC 数据校验实例解析
下一篇:JDBC连接MySQL数据库批量插入数据过程详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~