• 大小: 287KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: 其他
  • 标签: 文件上传  

资源简介

jsupload实现js上传文件,并显示文件上传进度

资源截图

代码片段和文件信息

package com.sohu.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.FileUploadbase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class Upload
 */
@WebServlet(“/Upload“)
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */   
    public Upload() {
        // TODO Auto-generated constructor stub
     super();
    }
  
/**
 * @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
doPost(request response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
response.setContentType(“text/html“);
//设置字符编码为UTF-8,支持汉字显示
response.setCharacterEncoding(“UTF-8“);
final long MAX_SIZE = 1024 * 1024 * 1024;//设置上传文件最大为1G

//上传文件路径
String filePath = “e:\\upload\\“;
String tempPath = “e:\\upload\\temp\\“;

//jsonp上传
String jsonp = request.getParameter(“callback“);

// 实例化一个硬盘文件工厂用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(1024 * 5);// 设置上传文件时用于临时存放文件的内存大小这里是5k.多于的部分将临时存在硬盘
dfif.setRepository(new File(tempPath));// 设置存放临时文件的目录

//用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
//设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);

PrintWriter out = response.getWriter();
final HttpSession session = request.getSession();

sfu.setProgressListener(new ProgressListener() {
private long temp = -1;
    public void update(long readBytes long totalBytes int item) {
long size = readBytes / 1024 * 1024 * 10;
if(temp == size){
return;
}
temp = size;
if (readBytes != -1) {
session.setAttribute(“readBytes“ ““ + readBytes);
    session.setAttribute(“totalBytes“ ““ + totalBytes);
}
    }
});

//从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {//处理文件尺寸过大异常
if (e instanceof SizeLimitE

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-01-30 16:15  jsupload\
     文件         601  2012-01-30 09:17  jsupload\.classpath
     文件        1041  2012-01-29 18:32  jsupload\.project
     目录           0  2012-01-30 16:15  jsupload\.settings\
     文件         503  2012-01-29 18:32  jsupload\.settings\.jsdtscope
     文件         395  2012-01-29 18:32  jsupload\.settings\org.eclipse.jdt.core.prefs
     文件         454  2012-01-29 18:32  jsupload\.settings\org.eclipse.wst.common.component
     文件         305  2012-01-29 18:32  jsupload\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2012-01-29 18:32  jsupload\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2012-01-29 18:32  jsupload\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2012-01-30 16:15  jsupload\src\
     目录           0  2012-01-30 16:15  jsupload\src\com\
     目录           0  2012-01-30 16:15  jsupload\src\com\sohu\
     目录           0  2012-01-30 16:15  jsupload\src\com\sohu\action\
     文件        6414  2012-01-29 17:48  jsupload\src\com\sohu\action\Upload.java
     文件        2839  2012-01-20 16:48  jsupload\src\com\sohu\action\UploadFileMessage.java
     文件        1578  2012-01-29 17:00  jsupload\src\com\sohu\action\UploadProgress.java
     目录           0  2012-01-30 16:15  jsupload\WebContent\
     目录           0  2012-01-30 16:15  jsupload\WebContent\meta-INF\
     文件          39  2012-01-29 18:32  jsupload\WebContent\meta-INF\MANIFEST.MF
     目录           0  2012-01-30 16:15  jsupload\WebContent\page\
     目录           0  2012-01-30 16:15  jsupload\WebContent\page\script\
     文件       10511  2012-01-30 15:20  jsupload\WebContent\page\script\jsupload.js
     文件        6505  2012-01-30 14:27  jsupload\WebContent\page\upload.jsp
     目录           0  2012-01-30 16:15  jsupload\WebContent\WEB-INF\
     目录           0  2012-01-30 16:15  jsupload\WebContent\WEB-INF\classes\
     目录           0  2012-01-30 16:15  jsupload\WebContent\WEB-INF\classes\com\
     目录           0  2012-01-30 16:15  jsupload\WebContent\WEB-INF\classes\com\sohu\
     目录           0  2012-01-30 16:15  jsupload\WebContent\WEB-INF\classes\com\sohu\action\
     文件        1325  2012-01-30 09:19  jsupload\WebContent\WEB-INF\classes\com\sohu\action\Upload$1.class
     文件        5702  2012-01-30 09:19  jsupload\WebContent\WEB-INF\classes\com\sohu\action\Upload.class
............此处省略7个文件信息

评论

共有 条评论