package cn.bluejoe.elfinder.controller.executors;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.log4j.Logger;
import org.json.JSONObject;
import cn.bluejoe.elfinder.controller.MultipleUploadItems;
import cn.bluejoe.elfinder.controller.executor.AbstractJsonCommandExecutor;
import cn.bluejoe.elfinder.controller.executor.CommandExecutor;
import cn.bluejoe.elfinder.controller.executor.FsItemEx;
import cn.bluejoe.elfinder.service.FsItemFilter;
import cn.bluejoe.elfinder.service.FsService;
public class UploadCommandExecutor extends AbstractJsonCommandExecutor
implements CommandExecutor
{
Logger _logger = Logger.getLogger(this.getClass());
// large file will be splitted into many parts
class Part
{
long _start;
long _size;
FileItemStream _content;
public Part(long start, long size, FileItemStream fileItemStream)
{
super();
this._start = start;
this._size = size;
this._content = fileItemStream;
}
}
// a large file with many parts
static class Parts
{
public static synchronized Parts getOrCreate(
HttpServletRequest request, String chunkId, String fileName,
long total, long totalSize)
{
//chunkId is not an unique number for files uploaded in one upload form
String key = String.format("chunk_%s_%s", chunkId, fileName);
// stores chunks in application context
Parts parts = (Parts) request.getServletContext().getAttribute(key);
if (parts == null)
{
parts = new Parts(chunkId, fileName, total, totalSize);
request.getServletContext().setAttribute(key, parts);
}
return parts;
}
private String _chunkId;
// number of parts
private long _numberOfParts;
private long _totalSize;
private String _fileName;
// all chunks
Map<Long, Part> _parts = new HashMap<Long, Part>();
public Parts(String chunkId, String fileName, long numberOfParts,
long totalSize)
{
_chunkId = chunkId;
_fileName = fileName;
_numberOfParts = numberOfParts;
_totalSize = totalSize;
}
public synchronized void addPart(long partIndex, Part part)
{
_parts.put(partIndex, part);
}
public boolean isReady()
{
return _parts.size() == _numberOfParts;
}
public InputStream openInputStream() throws IOException
{
return new InputStream()
{
long partIndex = 0;
Part part = _parts.get(partIndex);
InputStream is = part._content.openStream();
@Override
public int read() throws IOException
{
while (true)
{
// current part is not read completely
int c = is.read();
if (c != -1)
{
return c;
}
// next part?
if (partIndex == _numberOfParts - 1)
{
is.close();
return -1;
}
part = _parts.get(++partIndex);
is.close();
is = part._content.openStream();
}
}
};
}
public void checkParts() throws IOException
{
long totalSize = 0;
for (long i = 0; i < _numberOfParts; i++)
{
Part part = _parts.get(i);
totalSize += part._size;
}
if (totalSize != _totalSize)
throw new IOException(String.format(
"invalid file size: excepted %d, but is %d",
_totalSize, totalSize));
}
public void removeFromApplicationContext(HttpServletRequest request)
{
String key = String.format("chunk_%s_%s", _chunkId, _fileName);
request.getServletContext().removeAttribute(key);
}
}
interface FileWriter
{
FsItemEx createAndSave(String fileName, InputStream is)
throws IOException;
}
@Override
public void execute(FsService fsService, HttpServletRequest request,
ServletContext servletContext, JSONObject json) throws Exception
{
MultipleUploadItems uploads = MultipleUploadItems.loadFrom(request);
final List<FsItemEx> added = new ArrayList<FsItemEx>();
String target = request.getParameter("target");
final FsItemEx dir = super.findItem(fsService, target);
final FsItemFilter filter = getRequestedFilter(request);
FileWriter fw = new FileWriter()
{
@Override
public FsItemEx createAndSave(String fileName, InputStream is)
throws IOException
{
// fis.getName() returns full path such as 'C:\temp\abc.txt' in
// IE10
// while returns 'abc.txt' in Chrome
// see
// https://github.com/bluejoe2008/elfinder-2.x-servlet/issues/22
java.nio.file.Path p = java.nio.file.Paths.get(fileName);
FsItemEx newFile = new FsItemEx(dir, p.getFileName().toString());
/*
* String fileName = fis.getName(); FsItemEx newFile = new
* FsItemEx(dir, fileName);
*/
newFile.createFile();
newFile.writeStream(is);
if (filter.accepts(newFile))
added.add(newFile);
return newFile;
}
};
// chunked upload
if (request.getParameter("cid") != null)
{
processChunkUpload(request, uploads, fw);
}
else
{
processUpload(uploads, fw);
}
json.put("added", files2JsonArray(request, added));
}
private void processChunkUpload(HttpServletRequest request,
MultipleUploadItems uploads, FileWriter fw)
throws NumberFormatException, IOException
{
// cid : unique id of chunked uploading file
String cid = request.getParameter("cid");
// solr-5.5.2.tgz.48_65.part
String chunk = request.getParameter("chunk");
// 100270176,2088962,136813192
String range = request.getParameter("range");
String[] tokens = range.split(",");
Matcher m = Pattern.compile("(.*)\\.([0-9]+)\\_([0-9]+)\\.part")
.matcher(chunk);
if (m.find())
{
String fileName = m.group(1);
long index = Long.parseLong(m.group(2));
long total = Long.parseLong(m.group(3));
Parts parts = Parts.getOrCreate(request, cid, fileName, total + 1,
Long.parseLong(tokens[2]));
long start = Long.parseLong(tokens[0]);
long size = Long.parseLong(tokens[1]);
_logger.debug(String.format("uploaded part(%d/%d) of file: %s",
index, total, fileName));
parts.addPart(index, new Part(start, size, uploads
.items("upload[]").get(0)));
_logger.debug(String.format(">>>>%d", parts._parts.size()));
if (parts.isReady())
{
parts.checkParts();
_logger.debug(String.format("file is uploadded completely: %s",
fileName));
fw.createAndSave(fileName, parts.openInputStream());
// remove from application context
parts.removeFromApplicationContext(request);
}
}
}
private void processUpload(MultipleUploadItems uploads, FileWriter fw)
throws IOException
{
for (FileItemStream fis : uploads.items("upload[]"))
{
fw.createAndSave(fis.getName(), fis.openStream());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
著名web文件管理器elfinder的java后端,支持自定义文件视图、自定义文件操作;elfinder-2.x-servlet 为 elfinder-2.x connector 实现了 Java Servlet;elfinder 是一个用于 Web 的开源文件管理器,使用 jQuery 和 jQuery UI 用 JavaScript 编写。
资源推荐
资源详情
资源评论
收起资源包目录
elfinder-2.x-servlet-master.zip (61个子文件)
elfinder-2.x-servlet-master
23205811_gr0b.png 137KB
pom.xml 6KB
23205833_rxSV.png 123KB
.gitattributes 416B
src
main
resources
mime.types 14KB
log4j.properties 497B
java
cn
bluejoe
elfinder
localfs
LocalFsVolume.java 6KB
LocalFsItem.java 644B
servlet
ConnectorServlet.java 4KB
controller
executor
FsItemEx.java 4KB
DefaultCommandExecutorFactory.java 1KB
AbstractJsonCommandExecutor.java 2KB
AbstractCommandExecutor.java 6KB
CommandExecutor.java 171B
CommandExecutorFactory.java 135B
CommandExecutionContext.java 431B
FsException.java 258B
MultipleUploadItems.java 2KB
executors
UploadCommandExecutor.java 7KB
TreeCommandExecutor.java 980B
ParentsCommandExecutor.java 1KB
TmbCommandExecutor.java 2KB
OpenCommandExecutor.java 2KB
GetCommandExecutor.java 972B
RmCommandExecutor.java 1KB
SizeCommandExecutor.java 992B
MissingCommandExecutor.java 858B
LsCommandExecutor.java 1KB
FileCommandExecutor.java 3KB
PasteCommandExecutor.java 1KB
MkfileCommandExecutor.java 1KB
PutCommandExecutor.java 981B
DimCommandExecutor.java 2KB
RenameCommandExecutor.java 1KB
DuplicateCommandExecutor.java 2KB
SearchCommandExecutor.java 909B
MkdirCommandExecutor.java 947B
ConnectorController.java 6KB
ErrorException.java 841B
service
FsSecurityChecker.java 326B
FsServiceFactory.java 813B
FsItemFilter.java 396B
FsVolume.java 2KB
FsServiceConfig.java 95B
FsService.java 926B
FsItem.java 88B
impl
FsSecurityCheckerChain.java 1KB
StaticFsServiceFactory.java 716B
FsSecurityCheckFilterMapping.java 589B
FsSecurityCheckForAll.java 998B
DefaultFsService.java 5KB
DefaultFsServiceConfig.java 313B
util
FsItemFilterUtils.java 2KB
FsServiceUtils.java 471B
MimeTypesUtils.java 1KB
webapp
WEB-INF
web.xml 605B
web.xml.1 655B
elfinder-servlet.xml 4KB
index.html 1KB
LICENSE 1KB
.gitignore 3KB
共 61 条
- 1
资源评论
- abcdefg12512023-07-07发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6656
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功