Spring CommonsMultipartResolver 上传文件

19 篇文章 0 订阅

Controller:

@Controller
public class UploadController {
	@RequestMapping("upload")
	public String upload(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap){
		try {
			response.setContentType("text/html;charset=UTF-8");
		    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		    MultipartFile ffile = multipartRequest.getFile("file");
		    String path="";
		    if(ffile != null){
		        String ffileName = ffile.getOriginalFilename();
		        String tfileName = UUID.randomUUID().toString()+ffileName.substring(ffileName.lastIndexOf('.'), ffileName.length());
		        path=request.getSession().getServletContext().getRealPath("")+File.separator+"upload"+File.separator+tfileName;
		        File tfile = new File(path);
		        ffile.transferTo(tfile);
		    }
		    modelMap.put("result", "上传成功, "+path);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("result", "上传失败");
		}
		return "fileupload";
	}}


 

配置文件:

<?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:p="http://www.springframework.org/schema/p"     
         xmlns:context="http://www.springframework.org/schema/context"     
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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">   
      
   <context:annotation-config />   
        <!-- 把标记了@Controller注解的类转换为bean -->     
       <context:component-scan base-package="com.mvc.controller" />     
   <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->     
       <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />     
         
        <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->     
        <!-- 方法1 -->
<!--        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"     -->
<!--           p:prefix="/WEB-INF/view/" p:suffix=".jsp" />     -->
       <!-- 方法1 -->     
    <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename">
            <value>spring-views</value><!-- src下面spring-views.properties -->
        </property>
    </bean>        
            <!-- Spring的配置文件中添加-->
		<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
			<property name="defaultEncoding" value="UTF-8" />
		</bean> 
  </beans>


 

upload.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>上传例子</title>
  </head>
  <body>
    <div><font style="color:red;"><c:out value="${result}"/></font></div>
	<form id="fileUploadForm" method="post" action="upload.do" enctype="multipart/form-data">
		<tr>
		<td>File:</td>
		<td><input type="file" name="file"></td>
		</tr>
		<tr>
		<td><input type="submit" value="submit" ></td>
		</tr>
	</form>
  </body>
</html>


spring-views.properties:

fileupload.(class)=org.springframework.web.servlet.view.JstlView
fileupload.url=/upload.jsp


 

具体源码可在资源里面下载 --- 源码地址
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值