springMVC实现前台带进度条文件上传的示例代码

项目框架采用spring+hibernate+springMVC如果上传文件不想使用flash那么你可以采用HTML5;截图前段模块是bootstarp框架;不废话直接来代码;spring-mvc配置文件;效果截图如下:

详细实现如下:

1、mvc-config.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: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.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

  <!-- 注解扫描包 -->
  <context:component-scan base-package="com.yunfang.banks.controllers" /> 

  <!-- 开启注解 -->
  <mvc:annotation-driven />
  <!-- 不拦截静态资源 -->
  <mvc:default-servlet-handler />
  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/js/" />
      <mvc:mapping path="/js/**" />
      <bean class="com.yunfang.banks.intreceptor.MyInterceptor"></bean>
    </mvc:interceptor>
  </mvc:interceptors>
  <mvc:interceptors>
    <bean class="com.yunfang.banks.intreceptor.MyInterceptor"></bean>
  </mvc:interceptors>
  <!-- 静态资源(js/image)的访问 -->
  <mvc:resources location="/js/" mapping="/js/**" /> 

  <!-- 定义视图解析器 -->
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=""></property>
  </bean> 

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
      <list>
        <!--下载的时候专用 -->
        <bean
          class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
        <bean id="jsonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
          <property name="supportedMediaTypes">
            <list>
              <value>text/plain;charset=UTF-8</value>
            </list>
          </property>
        </bean>
      </list>
    </property>
  </bean>
  <!-- 配置springMVC处理上传文件的信息,自定义上传,带进度条 -->
  <bean id="multipartResolver" class="com.yunfang.banks.listener.CustomMultipartResolver">
      <property name="defaultEncoding" value="utf-8" />
    <property name="maxUploadSize" value="10000000000" />
  </bean>
    <!--
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" />
    <property name="maxUploadSize" value="10485760000" />
    <property name="maxInMemorySize" value="40960" />
  </bean>
    --> 

</beans>

2、实体工具类:Progress.Java

package com.yunfang.banks.listener; 

public class Progress { 

  private long pBytesRead;
  private long pContentLength;
  private long pItems;
  public long getpBytesRead() {
    return pBytesRead;
  }
  public void setpBytesRead(long pBytesRead) {
    this.pBytesRead = pBytesRead;
  }
  public long getpContentLength() {
    return pContentLength;
  }
  public void setpContentLength(long pContentLength) {
    this.pContentLength = pContentLength;
  }
  public long getpItems() {
    return pItems;
  }
  public void setpItems(long pItems) {
    this.pItems = pItems;
  }
  @Override
  public String toString() {
    return "Progress [pBytesRead=" + pBytesRead + ", pContentLength="
        + pContentLength + ", pItems=" + pItems + "]";
  } 

}

3、文件上传进度监听类:FileUploadProgressListener.java

package com.yunfang.banks.listener; 

import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component; 

@Component
public class FileUploadProgressListener implements ProgressListener {
  private HttpSession session;
  public void setSession(HttpSession session){
    this.session=session;
    Progress status = new Progress();//保存上传状态
    session.setAttribute("status", status);
  }
  public void update(long pBytesRead, long pContentLength, int pItems) {
    Progress status = (Progress) session.getAttribute("status");
    try {
      Thread.sleep(5);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    status.setpBytesRead(pBytesRead);
    status.setpContentLength(pContentLength);
    status.setpItems(pItems);
    //System.out.println(">>>>>>>>>>>>>>>>>>>>"+status); 

  } 

}

4、自定义扩展org.springframework.web.multipart.commons.CommonsMultipartResolver类,重写public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException方法:

CustomMultipartResolver.java

package com.yunfang.banks.listener; 

import java.util.List; 

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 

public class CustomMultipartResolver extends CommonsMultipartResolver {
  @Autowired
  private FileUploadProgressListener progressListener; 

  public void setFileUploadProgressListener(
      FileUploadProgressListener progressListener) {
    this.progressListener = progressListener;
  } 

  @Override
  @SuppressWarnings("unchecked")
  public MultipartParsingResult parseRequest(HttpServletRequest request)
      throws MultipartException {
    String encoding = determineEncoding(request);
    FileUpload fileUpload = prepareFileUpload(encoding);
    progressListener.setSession(request.getSession());
    fileUpload.setProgressListener(progressListener);
    try {
      List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
      return parseFileItems(fileItems, encoding);
    }
    catch (FileUploadBase.SizeLimitExceededException ex) {
      throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
    }
    catch (FileUploadException ex) {
      throw new MultipartException("Could not parse multipart servlet request", ex);
    }
  } 

}

5、uploadController.java

@RequestMapping("UserControllers/progress")
public void uploadFile(HttpServletRequest request,HttpServletResponse response,
            @RequestParam("file") CommonsMultipartFile file) throws IOException {
   response.setContentType("text/html");
   response.setCharacterEncoding("GBK");
   PrintWriter out;
   boolean flag = false;
   if (file.getSize() > 0) {
     //文件上传的位置可以自定义
     flag = FileUploadUtil.upLoadFile(file, request);
   }
   out = response.getWriter();
   if (flag == true) {
    out.print("1");
   } else {
    out.print("2");
   }
}

6、FileUploadUtil.java

import java.io.File; 

import javax.servlet.http.HttpServletRequest; 

import org.springframework.web.multipart.MultipartFile; 

public class FileUploadUtil { 

  public static Boolean uploadFile(HttpServletRequest request, MultipartFile file) {
     System.out.println("开始");
     String path = request.getSession().getServletContext().getRealPath("upload");
     String fileName = file.getOriginalFilename();
     System.out.println(path);
     File targetFile = new File(path, fileName);
     if (!targetFile.exists()) {
       targetFile.mkdirs();
     }
     // 保存
     try {
       file.transferTo(targetFile);
       return true;
     } catch (Exception e) {
       e.printStackTrace();
       return false;
     } 

  } 

}

7、前台页面

<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
      + request.getServerName() + ":" + request.getServerPort()
      + path + "/";
%> 

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"> 

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
 <link rel="stylesheet" type="text/css" href="styles.css">
-->
<link href="css/bootstrap.min.css" rel="stylesheet"> 

<style type="text/css">
.file-box {
  position: relative;
  width: 340px
} 

.txt {
  height: 22px;
  border: 1px solid #cdcdcd;
  width: 180px;
  vertical-align: middle;
  margin: 0;
  padding: 0
} 

.btn {
  border: 1px solid #CDCDCD;
  height: 24px;
  width: 70px;
  vertical-align: middle;
  margin: 0;
  padding: 0
} 

.file {
  position: absolute;
  top: 0;
  right: 80px;
  height: 24px;
  filter: alpha(opacity :  0);
  opacity: 0;
  width: 260px;
  vertical-align: middle;
  margin: 0;
  padding: 0
}
</style>
<%--<script type="text/javascript">
    function myInterval()
    {
      $("#progress").html("");
      $.ajax({
        type: "POST",
        url: "<%=basePath%>UserControllers/getSessions",
      data : "1=1",
      dataType : "text",
      success : function(msg) {
        var data = msg;
        console.log(data);
        $("#pdiv").css("width", data + "%");
        $("#progress").html(data + "%");
      }
    });
  }
  function autTime() {
    setInterval("myInterval()", 200);//1000为1秒钟
  }
</script> 

--%>
<script type="text/javascript">
  function UpladFile() {
    var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
    var FileController = "UserControllers/progress"; // 接收上传文件的后台地址
    // FormData 对象---进行无刷新上传
    var form = new FormData();
    form.append("author", "hooyes"); // 可以增加表单数据
    form.append("file", fileObj); // 文件对象
    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.open("post", FileController, true);
    xhr.onload = function() {
      alert("上传完成!");
      //$('#myModal').modal('hide');
    };
        //监听progress事件
    xhr.upload.addEventListener("progress", progressFunction, false);
    xhr.send(form);
  }
  function progressFunction(evt) { 

    var progressBar = document.getElementById("progressBar"); 

    var percentageDiv = document.getElementById("percentage"); 

    if (evt.lengthComputable) { 

      progressBar.max = evt.total; 

      progressBar.value = evt.loaded; 

      percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100)
          + "%"; 

    } 

  }
</script> 

</head> 

<body style="width: 80%;height: 80%;margin: 0px auto;">
  <div class="row bootstrap-admin-no-edges-padding">
    <div class="col-md-12">
      <div class="panel panel-default">
        <div class="panel-heading">
          <div class="text-muted bootstrap-admin-box-title">文件管理</div>
        </div>
        <div class="bootstrap-admin-panel-content">
          <button class="btn btn-primary btn-lg" data-toggle="modal"
            data-target="#myModal">上传</button>
          <input type="text" id="test">
        </div>
      </div>
    </div>
  </div> 

  <!-- 模态框(Modal) -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal"
            aria-hidden="true">×</button>
          <h4 class="modal-title" id="myModalLabel">文件上传进度</h4>
        </div>
        <div class="modal-body">
          <progress id="progressBar" value="0" max="100"
            style="width: 100%;height: 20px; "> </progress>
          <span id="percentage" style="color:blue;"></span> <br>
          <br>
          <div class="file-box">
            <input type='text' name='textfield' id='textfield' class='txt' />
            <input type='button' class='btn' value='浏览...' /> <input
              type="file" name="file" class="file" id="file" size="28"
              onchange="document.getElementById('textfield').value=this.value" />
            <input type="submit" name="submit" class="btn" value="上传"
              onclick="UpladFile()" /> 

          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">关闭
          </button>
        </div>
      </div>
      <!-- /.modal-content -->
    </div>
    <!-- /.modal -->
  </div>
  <script type="text/javascript"
    src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • springMVC配置环境实现文件上传和下载

    最近的项目中用到了文件的上传和下载功能,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试. 下面就是springMVC配置环境实现文件上传和下载的具体步骤,供大家参考,具体内容如下 一. 基础配置: maven导包及配置pom.xml,导包时除开springmvc的基础依赖外,需要导入文件上传下载时用到的commons-io.jsr和commons-fileupload.jar: <project xmlns="http://maven.apache.org/POM/4.0.0&

  • SpringMVC 文件上传配置,多文件上传,使用的MultipartFile的实例

    基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下如何使用SpringMVC进行表单上的文件上传以及多个文件同时上传的步骤 文件上传项目的源码下载地址:demo 一.配置文件: SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->

  • MyBatis与SpringMVC相结合实现文件上传、下载功能

    环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何使用input上传文件到服务器指定目录,或保存到数据库中:如何从数据库下载文件,和显示图像文件并实现缩放. 将文件存储在数据库中,一般是存文件的byte数组,对应的数据库数据类型为blob. 首先要创建数据库,此处使用MySql数据库. 注意:文中给出的代码多为节选重要片段,并不齐全. 1. 前期准备 使用maven创建一个springMVC+spring+mybatis+mysql的项目

  • 详解SpringMVC使用MultipartFile实现文件的上传

    如果需要实现跨服务器上传文件,就是将我们本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传.这里是结合SpringMVC+ftp的形式上传的.我们需要先懂得如何配置springMVC,然后在配置ftp,最后再结合MultipartFile上传文件. springMVC上传需要几个关键jar包,spring以及关联包可以自己配置,这里主要说明关键的jar包 1:spring-web-3.2.9.RELEASE.jar (spring的关键jar包,版本可以自己选择) 2:commons-

  • Spring MVC环境中文件上传功能的实现方法详解

    前言 我们在实际开发过程中,尤其是web项目开发,文件上传和下载的需求的功能非常场景,比如说用户头像.商品图片.邮件附件等等.其实文件上传下载的本质都是通过流的形式进行读写操作,而在开发中不同的框架都会对文件上传和下载有或多或少的封装,这里就以Spring MVC环境中文件的上传为例,讲解Spirng MVC环境下的文件上传功能实现.下面话不多说了,来一起看看详细的介绍吧. 一.客户端编程 由于多数文件上传都是通过表单形式提交给后台服务器的,因此,要实现文件上传功能,就需要提供一个文件上传的表单

  • SpringMVC文件上传的配置实例详解

    记述一下步骤以备查. 准备工作: 需要把Jakarta Commons FileUpload及Jakarta Commons io的包放lib里. 我这边的包是: commons-fileupload-1.1.1.jar commons-io-1.3.2.jar 然后在spring-servlet.xml进行multipartResolver配置,不配置好上传会不好用. <bean id="multipartResolver" class="org.springfram

  • SpringMVC文件上传 多文件上传实例

    必须明确告诉DispatcherServlet如何处理MultipartRequest.SpringMVC中提供了文件上传使用方式如下配置xxx-servlet.xml,添加如下代码: 复制代码 代码如下: <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <!-- 设置

  • springMVC结合AjaxForm上传文件

    最近在项目中需要上传文件文件,之前一直都是form提交的,尝试了一下AjaxForm,感觉还比较好用,写篇随笔mark下,供以后使用. 准备工作: 下载jquery-form.js 相关jar: commons-fileupload-1.1.1.jar commons-io-1.3.2.jar 在spring-servlet.xml进行multipartResolver配置: <bean id="multipartResolver" class="org.springf

  • Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!--配置下载返回类型--> <bean class="or

  • Spring MVC中上传文件实例

    SpringMVC(注解)上传文件需要注意的几个地方: 1.form的enctype="multipart/form-data",这个是上传文件必须的 2.applicationContext.xml配置: 复制代码 代码如下: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.w

随机推荐