0
点赞
收藏
分享

微信扫一扫

springMVC文件上传

有点d伤 2023-04-23 阅读 76


<%@ 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>Insert title here</title>
</head>
<body>
 
 
 
<form method="post" action="fileUpload.do"enctype="multipart/form-data">
 
<input  type="file" name="files">
<input  type="file" name="files">
<input  type="file" name="files">
 
<input type="submit" value="提交">
</form>

Spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context"   
xmlns:mvc="http://www.springframework.org/schema/mvc"   
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   
   
 
 
 
<bean id="internalResourceView"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
 
 
<mvc:annotation-driven/>
 
<context:component-scan base-package="springmvc"/>
 
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="200000"/>
</bean>
 
 
 
   
</beans>

控制器的编写

@RequestMapping("fileUpload")
   public void fileUpload(HttpServletRequest request,@RequestParam MultipartFile[] files) throwsIllegalStateException, IOException{
     
     
      System.out.println("file uploading begin ...");
     
      for (MultipartFile file : files) {
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        System.out.println(file.getContentType());
       
       
"upload");
       
        file.transferTo(newFile(path+File.separator+file.getOriginalFilename()));
       
       
      }
     
     
     
     
      System.out.println("file uploading end ...");
     
     
     
     
   }

 

举报

相关推荐

0 条评论