zoukankan      html  css  js  c++  java
  • jsp 界面上传单个文件

    1...pom.xml 

    1  <dependency>
    2           <groupId>commons-fileupload</groupId>
    3           <artifactId>commons-fileupload</artifactId>
    4           <version>1.3.1</version>
    5       </dependency>

    2...上传xml 配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans
     7        http://www.springframework.org/schema/beans/spring-beans.xsd
     8        http://www.springframework.org/schema/context
     9        http://www.springframework.org/schema/context/spring-context.xsd
    10        http://www.springframework.org/schema/mvc
    11        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     <!--开启注解  扫描包-->
    13     <context:component-scan base-package="com.wsc"></context:component-scan>
    14     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    15         <property name="prefix" value="/WEB-INF/"></property>
    16         <property name="suffix" value=".jsp"></property>
    17     </bean>
    18     <!-- 引入注解驱动-->
    19     <mvc:annotation-driven></mvc:annotation-driven>
    20     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    21         <property name="maxUploadSize" value="5242880"/>
    22 
    23     </bean>
    24 </beans>
    springmvc.xml

    3...web.xml

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 <web-app>
     5   <display-name>Archetype Created Web Application</display-name>
     6     <filter>
     7         <filter-name>characterEncodingFilter</filter-name>
     8         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
     9         <init-param>
    10             <param-name>encoding</param-name>
    11             <param-value>utf-8</param-value>
    12         </init-param>
    13     </filter>
    14     <filter-mapping>
    15         <filter-name>characterEncodingFilter</filter-name>
    16         <url-pattern>/*</url-pattern>
    17     </filter-mapping>
    18     <servlet>
    19         <servlet-name>DispatcherServlet</servlet-name>
    20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    21         <init-param>
    22             <param-name>contextConfigLocation</param-name>
    23             <param-value>classpath:springmvc.xml</param-value>
    24         </init-param>
    25 <!--        启动tomcat按照顺序加载-->
    26         <load-on-startup>1</load-on-startup>
    27     </servlet>
    28     <servlet-mapping>
    29         <servlet-name>DispatcherServlet</servlet-name>
    30         <url-pattern>/</url-pattern>
    31     </servlet-mapping>
    32 
    33 </web-app>
    View Code

    4...jsp 代码

     1 <%--
     2   Created by IntelliJ IDEA.
     3   User: Administrator
     4   Date: 2019/7/9
     5   Time: 10:10
     6   To change this template use File | Settings | File Templates.
     7 --%>
     8 <%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
     9 <html>
    10 <head>
    11     <title>Title</title>
    12 </head>
    13 <body>
    14 <a href="${pageContext.request.contextPath}/test/student1">请求1</a>
    15 <a href="${pageContext.request.contextPath}/test/student">请求2</a>
    16 <form action="${pageContext.request.contextPath}/test/student" method="post">
    17     <input type="hidden" name="id" value="1"><br>
    18     <input type="submit" value="提交">
    19 </form>
    20 <form action="${pageContext.request.contextPath}/test/testUpload" method="post" enctype="multipart/form-data"><!--必须设置enctype="multipart/form-data"--> 
    21 姓名:<input type="text" name="userName"><br/>
    22 <input type="file" name="uploadFile"><br/>
    23 <input type="submit" value="上传"><br/>
    24 </form>
    25 </body>
    26 </html>
    name值与java代码的参数一致

    5...java代码

     1 package com.wsc.servlet;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.multipart.MultipartFile;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import java.io.File;
     9 import java.io.IOException;
    10 import java.util.UUID;
    11 
    12 /**
    13  * @version 1.0
    14  * @ClassName UploadController
    15  * @Description TODO
    16  * @Author WSC
    17  * @Date 2019/7/11 10:16
    18  **/
    19 @Controller
    20 @RequestMapping("/test")
    21 public class UploadController {
    22     @RequestMapping("/testUpload")
    23     public String testUpload(String userName, MultipartFile uploadFile, HttpServletRequest request){
    24         // 1 路径
    25         String realPath = request.getSession().getServletContext().getRealPath("/upload");
    26         File realFile = new File(realPath);
    27         if(!realFile.exists()){
    28             realFile.mkdirs();
    29         }
    30         //2 文件的名称
    31         String replace = UUID.randomUUID().toString().replace("-", "");
    32         //  获取文件的真实名字
    33         String originalFilename = uploadFile.getOriginalFilename();
    34         System.out.println(originalFilename);
    35         // 唯一的名字
    36         String realName=replace+originalFilename;
    37         System.out.println(realName);
    38         try {
    39             // 文件上传
    40             uploadFile.transferTo(new File(realFile,realName));
    41         } catch (IOException e) {
    42             e.printStackTrace();
    43         }
    44         return "show";
    45     }
    46 }
    java代码

     

  • 相关阅读:
    e621. Activating a Keystroke When Any Child Component Has Focus
    e587. Filling Basic Shapes
    e591. Drawing Simple Text
    e595. Drawing an Image
    e586. Drawing Simple Shapes
    e636. Listening to All Key Events Before Delivery to Focused Component
    在 PL/SQL 块的哪部分可以对初始变量赋予新值? (选择1项)
    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
    你判断下面语句,有什么作用?(单选)
    Oracle数据库表空间与数据文件的关系描述正确的是( )
  • 原文地址:https://www.cnblogs.com/wangshichang/p/11365018.html
Copyright © 2011-2022 走看看