`
hzywy
  • 浏览: 165792 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

springmvc上传

阅读更多

    最近一直在网上看资料,今天我在网上发现一个demo,是基于springMVC开发的上传。由于本人刚好这两天在学习springMVC。以前用struts2.0 做过多文件上传的,现在我把这springMVC的多文件上传来分享给大家。我是根据http://zhoshijie.iteye.com/blog/1177390 给的demo完成的啊,希望原著能支持!!!我来简单讲解一下。


lib包

目录结构:


spring-servlet-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:p="http://www.springframework.org/schema/p"
    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/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">


    <!--  启动Spring MVC的注解功能,完成请求和注解POJO的映射  -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
   
 
   
   
    <context:component-scan base-package="com"/>       
   

    <!--  对模型视图名称的解析,在请求时模型视图名称添加前后缀  -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <!-- 文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8">
     <!-- 以字节为单位的最大上传文件的大小 -->
<!--     <property name="maxUploadSize" value="1000000" />-->
    </bean>
</beans>


UploadFileController.java******************************
package com.len.trans.controller;

import java.io.FileOutputStream;
import java.util.List;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.len.trans.common.Constant;
import com.len.trans.util.PropertiesUtil;

@Controller//声明该类为控制器类
@RequestMapping("/upload")
public class UploadFileController {   
   
    /**
     * 进入上传页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/toUpload")//将文件上传请求映射到该方法
    public ModelAndView toUpload() throws Exception{
        return new ModelAndView("/upload");//返回上传视图
    }
   
    /**
     * 文件上传
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/uploadFile",method=RequestMethod.POST)//将文件上传请求映射到该方法
    public ModelAndView uploadFile(MultipartHttpServletRequest request) throws Exception{       
        PropertiesUtil pUtil=PropertiesUtil.createPropertiesUtil(Constant.UPLOADPATH_FILE);
        List<MultipartFile> files=request.getFiles("file");//取得from里面的参数
        String uploadpath=request.getSession().getServletContext().getRealPath(pUtil.getProperty(Constant.UPLOADPATH_PATH));
        System.out.println("uploadpath :"+uploadpath);
        for (MultipartFile file : files) {
            if (file.isEmpty()) continue;
            FileOutputStream fileOS=new FileOutputStream(uploadpath+"/"+file.getOriginalFilename());
            fileOS.write(file.getBytes());
            fileOS.close();
        }
        return new ModelAndView("/success");//返回成功视图
    }   

一下这段主要是告诉大家另外一种传参的方式,上面是用request取的: @RequestParam("name") String name, //设置请求参数的名称和类型  @RequestParam("file") CommonsMultipartFile mFile


//     public String handleFormUpload(@RequestParam("name") String name, //设置请求参数的名称和类型
//               @RequestParam("file") CommonsMultipartFile mFile) { //请求参数一定要与form中的参数名对应
//              if (!mFile.isEmpty()) {
//               String path = this.servletContext.getRealPath("/tmp/");  //获取本地存储路径
//               File file = new File(path + new Date().getTime() + ".jpg"); //新建一个文件
//               try {
//                mFile.getFileItem().write(file); //将上传的文件写入新建的文件中
//               } catch (Exception e) {
//                e.printStackTrace();
//               }
//              }
}

upload.jsp**********************************************

<%@ 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>
<center>
    <form action="<%=request.getContextPath()%>/upload/uploadFile" method="post" enctype="multipart/form-data">
        <input type="hidden" name="_method" value="post">   
        文件1:<input type="file" name="file"><br>
        文件2:<input type="file" name="file"><br>
        <input type="submit" value=" 提 交 "><br>
    </form>
</center>
</body>
</html>

分享到:
评论
2 楼 NeverGiveUpToChange 2013-09-27  
nice,还不错哦,谢谢啦!!!
1 楼 小胖vs小猪 2012-04-05  
nice,还不错哦

相关推荐

Global site tag (gtag.js) - Google Analytics