zoukankan      html  css  js  c++  java
  • Spring MVC 之文件上传(七)

    SpringMVC同样使用了apache的文件上传组件。所以需要引入以下包:

    apache-commons-fileupload.jar

    apache-commons-io.jar

    在springAnnotation-servlet.xml中配置

    1 <!-- 定义文件上传解析器 -->
    2     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    3         <property name="maxUploadSize" value="-1"/><!-- -1代表不限制文件大小,单位是B -->
    4         <property name="defaultEncoding"  value="utf-8"/><!-- 设置字符集编码 -->
    5         <property name="maxInMemorySize" value="1024"/><!-- 内存中最大内存空间,单位是B NO KB -->
    6     </bean>

    控制器:

     1 package com.cy.springannotation.controller;
     2 
     3 import java.io.File;
     4 
     5 import javax.servlet.http.HttpServletRequest;
     6 
     7 import org.apache.log4j.Logger;
     8 import org.springframework.stereotype.Controller;
     9 import org.springframework.web.bind.annotation.RequestMapping;
    10 import org.springframework.web.bind.annotation.RequestMethod;
    11 import org.springframework.web.bind.annotation.RequestParam;
    12 import org.springframework.web.multipart.commons.CommonsMultipartFile;
    13 
    14 @Controller
    15 public class FileUploadController {
    16     private Logger log = Logger.getLogger(this.getClass());
    17     
    18     
    19     /**
    20      * CommonsMultipartFile file
    21      * @param file
    22      * @return
    23      */
    24     @RequestMapping(value="/upload.do",method=RequestMethod.POST)
    25     public String upload(@RequestParam("fileName") CommonsMultipartFile file,HttpServletRequest req){
    26         //获取原始文件名
    27         String fileName = file.getOriginalFilename();
    28         log.info(fileName);
    29         String path = req.getSession().getServletContext().getRealPath("upload");
    30         try {
    31             file.getFileItem().write(new File(path + File.separator + fileName));
    32         } catch (Exception e) {
    33             
    34             log.error(e);
    35         }
    36         return "success";
    37     }
    38     
    39 }

     上传文件页面:

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>文件上传</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26    <form action="upload.do" method="post" enctype="multipart/form-data">
    27         <table>
    28             <tr>
    29                 <td><input type="file" name="fileName"/></td>
    30             </tr>
    31             <tr>
    32                 <td><input type="submit" value="提交"/></td>
    33             </tr>
    34         </table>
    35     </form>
    36     
    37   </body>
    38 </html>

    上传文件就可以了!

  • 相关阅读:
    hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map
    hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
    hdu6438 Buy and Resell 买卖物品 ccpc网络赛 贪心
    hdu6441 Find Integer 求勾股数 费马大定理
    bzoj 1176 Mokia
    luogu 3415 祭坛
    bzoj 1010 玩具装箱
    bzoj 3312 No Change
    luogu 3383【模板】线性筛素数
    bzoj 1067 降雨量
  • 原文地址:https://www.cnblogs.com/hellokitty1/p/5167853.html
Copyright © 2011-2022 走看看