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

可能需要的jar包:

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

需要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字符串, 在客户端做更多处理
}
}
} }
上一篇:WebAPI通过multipart/form-data方式同时上传文件以及数据(含HttpClient上传Demo)


下一篇:Spring Mvc 上传文件Demo 实例