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

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

springMVC实现前台带进度条文件上传的示例代码_第1张图片

详细实现如下:

1、mvc-config.xml

 
 
 
   
   
 
   
   
   
   
   
     
       
       
       
     
   
   
     
   
   
   
 
   
   
     
     
   
 
   
     
       
         
         
         
           
             
              text/plain;charset=UTF-8 
             
           
         
       
     
   
   
   
       
     
   
     
   
 

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 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 + "/"; 
%> 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<%-- 
 
 
--%> 
 
 
 
 
 
  
文件管理

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

你可能感兴趣的:(springMVC实现前台带进度条文件上传的示例代码)