java http大文件上传,断点续传项目研究,Github上传源代码

1,项目调研

因为需要研究下断点上传的问题。找了很久终于找到一个比较好的项目。

在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面。

https://github.com/freewebsys/java-large-file-uploader-demo

效果:

上传中,显示进度,时间,百分比。

点击【Pause】暂停,点击【Resume】继续。

2,代码分析

原始项目:

https://code.google.com/p/java-large-file-uploader/

这个项目最后更新的时间是 2012 年,项目进行了封装使用最简单的方法实现了http的断点上传。

因为html5 里面有读取文件分割文件的类库,所以才可以支持断点上传,所以这个只能在html5 支持的浏览器上面展示。

同时,在js 和 java 同时使用 cr32 进行文件块的校验,保证数据上传正确。

代码在使用了最新的servlet 3.0 的api,使用了异步执行,监听等方法。

上传类UploadServlet

@Component("javaLargeFileUploaderServlet")
@WebServlet(name = "javaLargeFileUploaderServlet", urlPatterns = { "/javaLargeFileUploaderServlet" })
public class UploadServlet extends HttpRequestHandlerServlet
		implements HttpRequestHandler {

	private static final Logger log = LoggerFactory.getLogger(UploadServlet.class);

	@Autowired
	UploadProcessor uploadProcessor;

	@Autowired
	FileUploaderHelper fileUploaderHelper;

	@Autowired
	ExceptionCodeMappingHelper exceptionCodeMappingHelper;

	@Autowired
	Authorizer authorizer;

	@Autowired
	StaticStateIdentifierManager staticStateIdentifierManager;

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		log.trace("Handling request");

		Serializable jsonObject = null;
		try {
			// extract the action from the request
			UploadServletAction actionByParameterName =
					UploadServletAction.valueOf(fileUploaderHelper.getParameterValue(request, UploadServletParameter.action));

			// check authorization
			checkAuthorization(request, actionByParameterName);

			// then process the asked action
			jsonObject = processAction(actionByParameterName, request);

			// if something has to be written to the response
			if (jsonObject != null) {
				fileUploaderHelper.writeToResponse(jsonObject, response);
			}

		}
		// If exception, write it
		catch (Exception e) {
			exceptionCodeMappingHelper.processException(e, response);
		}

	}

	private void checkAuthorization(HttpServletRequest request, UploadServletAction actionByParameterName)
			throws MissingParameterException, AuthorizationException {

		// check authorization
		// if its not get progress (because we do not really care about authorization for get
		// progress and it uses an array of file ids)
		if (!actionByParameterName.equals(UploadServletAction.getProgress)) {

			// extract uuid
			final String fileIdFieldValue = fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId, false);

			// if this is init, the identifier is the one in parameter
			UUID clientOrJobId;
			String parameter = fileUploaderHelper.getParameterValue(request, UploadServletParameter.clientId, false);
			if (actionByParameterName.equals(UploadServletAction.getConfig) && parameter != null) {
				clientOrJobId = UUID.fromString(parameter);
			}
			// if not, get it from manager
			else {
				clientOrJobId = staticStateIdentifierManager.getIdentifier();
			}

			// call authorizer
			authorizer.getAuthorization(
					request,
					actionByParameterName,
					clientOrJobId,
					fileIdFieldValue != null ? getFileIdsFromString(fileIdFieldValue).toArray(new UUID[] {}) : null);

		}
	}

	private Serializable processAction(UploadServletAction actionByParameterName, HttpServletRequest request)
			throws Exception {
		log.debug("Processing action " + actionByParameterName.name());

		Serializable returnObject = null;
		switch (actionByParameterName) {
			case getConfig:
				String parameterValue = fileUploaderHelper.getParameterValue(request, UploadServletParameter.clientId, false);
				returnObject =
						uploadProcessor.getConfig(
								parameterValue != null ? UUID.fromString(parameterValue) : null);
				break;
			case verifyCrcOfUncheckedPart:
				returnObject = verifyCrcOfUncheckedPart(request);
				break;
			case prepareUpload:
				returnObject = prepareUpload(request);
				break;
			case clearFile:
				uploadProcessor.clearFile(UUID.fromString(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId)));
				break;
			case clearAll:
				uploadProcessor.clearAll();
				break;
			case pauseFile:
				List<UUID> uuids = getFileIdsFromString(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId));
				uploadProcessor.pauseFile(uuids);
				break;
			case resumeFile:
				returnObject =
						uploadProcessor.resumeFile(UUID.fromString(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId)));
				break;
			case setRate:
				uploadProcessor.setUploadRate(UUID.fromString(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId)),
						Long.valueOf(fileUploaderHelper.getParameterValue(request, UploadServletParameter.rate)));
				break;
			case getProgress:
				returnObject = getProgress(request);
				break;
		}
		return returnObject;
	}

	List<UUID> getFileIdsFromString(String fileIds) {
		String[] splittedFileIds = fileIds.split(",");
		List<UUID> uuids = Lists.newArrayList();
		for (int i = 0; i < splittedFileIds.length; i++) {
			uuids.add(UUID.fromString(splittedFileIds[i]));
		}
		return uuids;
	}

	private Serializable getProgress(HttpServletRequest request)
			throws MissingParameterException {
		Serializable returnObject;
		String[] ids =
				new Gson()
						.fromJson(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId), String[].class);
		Collection<UUID> uuids = Collections2.transform(Arrays.asList(ids), new Function<String, UUID>() {

			@Override
			public UUID apply(String input) {
				return UUID.fromString(input);
			}

		});
		returnObject = Maps.newHashMap();
		for (UUID fileId : uuids) {
			try {
				ProgressJson progress = uploadProcessor.getProgress(fileId);
				((HashMap<String, ProgressJson>) returnObject).put(fileId.toString(), progress);
			}
			catch (FileNotFoundException e) {
				log.debug("No progress will be retrieved for " + fileId + " because " + e.getMessage());
			}
		}
		return returnObject;
	}

	private Serializable prepareUpload(HttpServletRequest request)
			throws MissingParameterException, IOException {

		// extract file information
		PrepareUploadJson[] fromJson =
				new Gson()
						.fromJson(fileUploaderHelper.getParameterValue(request, UploadServletParameter.newFiles), PrepareUploadJson[].class);

		// prepare them
		final HashMap<String, UUID> prepareUpload = uploadProcessor.prepareUpload(fromJson);

		// return them
		return Maps.newHashMap(Maps.transformValues(prepareUpload, new Function<UUID, String>() {

			public String apply(UUID input) {
				return input.toString();
			};
		}));
	}

	private Boolean verifyCrcOfUncheckedPart(HttpServletRequest request)
			throws IOException, MissingParameterException, FileCorruptedException, FileStillProcessingException {
		UUID fileId = UUID.fromString(fileUploaderHelper.getParameterValue(request, UploadServletParameter.fileId));
		try {
			uploadProcessor.verifyCrcOfUncheckedPart(fileId,
					fileUploaderHelper.getParameterValue(request, UploadServletParameter.crc));
		}
		catch (InvalidCrcException e) {
			// no need to log this exception, a fallback behaviour is defined in the
			// throwing method.
			// but we need to return something!
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}
}

异步上传UploadServletAsync

@Component("javaLargeFileUploaderAsyncServlet")
@WebServlet(name = "javaLargeFileUploaderAsyncServlet", urlPatterns = { "/javaLargeFileUploaderAsyncServlet" }, asyncSupported = true)
public class UploadServletAsync extends HttpRequestHandlerServlet
		implements HttpRequestHandler {

	private static final Logger log = LoggerFactory.getLogger(UploadServletAsync.class);

	@Autowired
	ExceptionCodeMappingHelper exceptionCodeMappingHelper;

	@Autowired
	UploadServletAsyncProcessor uploadServletAsyncProcessor;

	@Autowired
	StaticStateIdentifierManager staticStateIdentifierManager;

	@Autowired
	StaticStateManager<StaticStatePersistedOnFileSystemEntity> staticStateManager;

	@Autowired
	FileUploaderHelper fileUploaderHelper;

	@Autowired
	Authorizer authorizer;

	/**
	 * Maximum time that a streaming request can take.<br>
	 */
	private long taskTimeOut = DateUtils.MILLIS_PER_HOUR;

	@Override
	public void handleRequest(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {

		// process the request
		try {

			//check if uploads are allowed
			if (!uploadServletAsyncProcessor.isEnabled()) {
				throw new UploadIsCurrentlyDisabled();
			}

			// extract stuff from request
			final FileUploadConfiguration process = fileUploaderHelper.extractFileUploadConfiguration(request);

			log.debug("received upload request with config: "+process);

			// verify authorization
			final UUID clientId = staticStateIdentifierManager.getIdentifier();
			authorizer.getAuthorization(request, UploadServletAction.upload, clientId, process.getFileId());

			//check if that file is not paused
			if (uploadServletAsyncProcessor.isFilePaused(process.getFileId())) {
				log.debug("file "+process.getFileId()+" is paused, ignoring async request.");
				return;
			}

			// get the model
			StaticFileState fileState = staticStateManager.getEntityIfPresent().getFileStates().get(process.getFileId());
			if (fileState == null) {
				throw new FileNotFoundException("File with id " + process.getFileId() + " not found");
			}

			// process the request asynchronously
			final AsyncContext asyncContext = request.startAsync();
			asyncContext.setTimeout(taskTimeOut);

			// add a listener to clear bucket and close inputstream when process is complete or
			// with
			// error
			asyncContext.addListener(new UploadServletAsyncListenerAdapter(process.getFileId()) {

				@Override
				void clean() {
					log.debug("request " + request + " completed.");
					// we do not need to clear the inputstream here.
					// and tell processor to clean its shit!
					uploadServletAsyncProcessor.clean(clientId, process.getFileId());
				}
			});

			// then process
			uploadServletAsyncProcessor.process(fileState, process.getFileId(), process.getCrc(), process.getInputStream(),
					new WriteChunkCompletionListener() {

						@Override
						public void success() {
							asyncContext.complete();
						}

						@Override
						public void error(Exception exception) {
							// handles a stream ended unexpectedly , it just means the user has
							// stopped the
							// stream
							if (exception.getMessage() != null) {
								if (exception.getMessage().equals("Stream ended unexpectedly")) {
									log.warn("User has stopped streaming for file " + process.getFileId());
								}
								else if (exception.getMessage().equals("User cancellation")) {
									log.warn("User has cancelled streaming for file id " + process.getFileId());
									// do nothing
								}
								else {
									exceptionCodeMappingHelper.processException(exception, response);
								}
							}
							else {
								exceptionCodeMappingHelper.processException(exception, response);
							}

							asyncContext.complete();
						}

					});
		}
		catch (Exception e) {
			exceptionCodeMappingHelper.processException(e, response);
		}

	}

}

3,请求流程图:

主要思路就是将文件切分,然后分块上传。

时间: 2024-11-09 04:48:03

java http大文件上传,断点续传项目研究,Github上传源代码的相关文章

用git上传本地项目到github上

首先确认自己已经安装了git,打开git bash,输入ssh-keygen -t rsa -C "自己的邮箱地址@XXX.com" ,生成自己的公钥与私钥   一路默认回车,会生成公钥.私钥到以下文件夹下id_rsa是私钥,id_rsa.pub是公钥,打开公钥等下要用到   浏览器进入自己的github,打开设置,进入ssh and GPG keys   点击NEW ssh key,自己填个标题,下面内容复制前面打开的公钥,最后添加   可以用ssh -T [email protec

通过Git Gui Here上传本地项目到GitHub上

1. 打开git Gui Here,选择 Create New Repository(在本地创建一个仓库),如下 2.选择你所要托管的项目本地存放的位置(即本地新建仓库的位置),如: 3.完成点击创建以后,会弹出可视化窗:点击Remote-->Add... 4. 弹出以下框以后输入你Github的用户名(如shanerou是我的github名称)和你需要将你本地的项目放在Github的远程仓库的地址,如我在Github上创建了一个repository名称为Test的,然后添加如下,点击创建,弹出

使用git上传本地项目到GitHub上和更新

最近在玩GitHub这个基友平台,在这里做个总结,方便以后查阅,也可以给网友们做个参考. 一:注册GitHub账号和下载git. 二:在GitHub上新建个仓库,点+号再点击New repository,给你的仓库起个名字,我的是test. 三:安装git,我安装的是win64版本,直接解压安装就好.在本地磁盘找一个文件夹作为本地仓库,选择Git Bash Here,切换到/e/te 目录下,输入git init初始化,发现目录下多了一个.git文件夹. 四:配置ssh key值  在git命令

第一次使用Git上传本地项目到github上

对于程序原来说都听说过GitHub,GitHub有许多开源的的项目和一些前沿的技术.因为自己在刚刚开始使用Git把自己写的一些小dome放到GitHub上遇到许多的坑,这么长时间过去了,想对第一次使用Git上传代码做一下总结,以免使自己忘记. 1.下载Git软件:https://git-scm.com/downloads,据说ios自带的有git软件,这个我就不太清楚了. 2.下载之后安装就很简单了,一路下一步就可以了.安装完成后鼠标右击和者开始->程序会出现,打开Git Bash,进入bash

git上传本地项目带Github上

创建好线上版本库以后就可以在本地进行上传 1.选择好文件夹右击Git Bash Here 2.先创建本地版本库 git init 3.git add README.md 4.git commit -m "add README" 5.git push -u origin master 6.git remote add origin 你的版本库链接 7.git add . 8.git commit -m "Initial commit" 9.git push -u or

idea上传本地项目到github上(图解)

注:作者省去了一些前期准备,比如安装git以及以及常见问题,截图仅为帮助同伴熟悉这个流程,PyCharm仅用以上步骤是不够的,更详细的内容可以参考其他博客 原文地址:https://blog.51cto.com/13646338/2481896

半小时学会上传本地项目到github

一.注册github账号   首先需要注册一个github账号,注册地址:https://github.com 接着会来到这 然后会收到一封github发的邮件,进入邮箱验证 二.创建个人的github仓库  验证成功是这样的,然后点击创建仓库 也可以来到个人中心里创建仓库 创建完成好之后是这样子的.创建仓库的时候最好别用中文,不然你的仓库名就会显示 -  这样一条小横线. 三. 配置SSH keys 终端里输入显示隐藏文件:defaults write com.apple.finder App

【原创】用JAVA实现大文件上传及显示进度信息

用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框架中使用类似CommonsMultipartFile对象处理表二进制文件信息. 而.NET 中使用HtmlInputFile/ HttpPostedFile对象处理二进制文件信息. 优点:使用框架内置对象可以很方便的

java+web+大文件上传下载

文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用HTML5的API,对文件上传进行渐进式增强:     * iframe上传  * ajax上传  * 进度条  * 文件预览  * 拖放上传 1.1 传统形式 文件上传的传统形式,是使用表单元素file,参考 http://www.ruanyifeng.com/blog/2012/08/file_

使用Git上传本地项目到GitHub/Coding/码云

Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. 本文以利用Git上传本地项目到GitHub为例,为那些刚刚接触Git的新手提供使用Git上传本地项目到GitHub/Coding/码云的方法. 注册GitHub账号并安装Git 首先注册GitHub账号,并添加新项目. 下载并安装Git,安装路径可以自己修改,安装过程中的一些设置选择默认即可. 利用Git上传本地项目到GitHub 在本地项目文件夹中空白处右键单击选择"Git Bash Here"