码迷,mamicode.com
首页 > 编程语言 > 详细

Java Miniui实现批量上传文件demo 201906221520

时间:2019-06-22 15:51:43      阅读:1180      评论:0      收藏:0      [点我收藏+]

标签:src   dtd   pos   date   返回   raw   dem   需要   extends   

可能需要的jar包:

技术图片

需要miniui(类似easyui).

Test2019062201.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
    <script src="scripts/boot.js" type="text/javascript"></script>
    <script src="fileupload/swfupload/swfupload.js" type="text/javascript"></script>
    <script src="multiupload/src/multiupload.js" type="text/javascript"></script>
    <link href="multiupload/src/multiupload.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <div id="multiupload1" class="uc-multiupload" style="width: 600px; height: 300px" 
        flashurl="fileupload/swfupload/swfupload.swf"
        uploadurl="UploadServlet" _autoUpload="true" _limittype="*.*"
        onuploaderror="onUploadError" onuploadsuccess="onUploadSuccess"
    >
    </div>

    <script type="text/javascript">
    
        var fileCount = 0;
         var grid = mini.get("multiupload1");
    
         mini.parse();
         
        function getData() {
            var data = grid.getData();
            var json = mini.encode(data);
            alert(json);
        }

        function onUploadSuccess(e) {
            fileCount++;
            var data = grid.getData();
            if(data.length == fileCount){
                mini.alert("全部文件上传成功!");    
            }
        }
        
        function onUploadError(e) {
            mini.alert("上传失败:" + e.file +   + e.message);
        }
    
    </script>

</body>
</html>

UploadServlet.java

package org.jimmy.testwebproject2019012602.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

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

import com.oreilly.servlet.MultipartRequest;

/**
 * @author ラピスラズリ(Dawn)
 * @date 2019年6月22日 下午3:10:01
 * @detail 批量上传文件
 */
public class UploadServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SuppressWarnings("rawtypes")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uploadPath = "upload";
        HttpSession session = request.getSession();
        String saveDirectory = session.getServletContext().getRealPath("/" + uploadPath);    
        MultipartRequest multi = new MultipartRequest(request, saveDirectory,
            100 * 1024 * 1024, "UTF-8");
        //如果有上传文件, 则保存到数据内
        Enumeration files = multi.getFileNames();
        while (files.hasMoreElements()) {
            String name = (String) files.nextElement();
            File f = multi.getFile(name);
            if(f != null){
                //读取上传后的项目文件, 导入保存到数据中
                String fileName = multi.getFilesystemName(name);
                response.getWriter().write(fileName + "(" + new Date() + ")");    //可以返回一个JSON字符串, 在客户端做更多处理                    
            }
        }
    }

}

 

Java Miniui实现批量上传文件demo 201906221520

标签:src   dtd   pos   date   返回   raw   dem   需要   extends   

原文地址:https://www.cnblogs.com/JimmySeraph/p/11069002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!