android应用基于局域网部署网页---上传图片

应用产生背景:最近公司要实现在TV端浏览用户照片的功能,之前通过拔插U盘/SD卡的方式,预先往用户文件夹存入图片。后来觉得这种方式特别繁琐,而且还得区分是哪个用户的文件夹(相册)。所以利用应用部署网页,采用扫描二维码方式上传图片

电视端:


手机页面:

android应用基于局域网部署网页---上传图片_第1张图片

部署网页利用到了AndServer,一个Android端的web服务器

框架github地址:https://github.com/yanzhenjie/AndServer

网页UI地址:https://github.com/Tencent/weui

将上传网页放入项目assets目录下

开启服务,部署网页

		mAssetManager = getAssets();

		AndServer andServer = new AndServer.Build().port(8080).timeout(10 * 1000)
				.registerHandler("upload", new PhotoUploadHandler(this))
				.website(new AssetsWebsite(mAssetManager, ""))
				.listener(mListener).build();

		mServer = andServer.createServer();
服务端保存图片

	FileItemFactory factory = new DiskFileItemFactory(1024 * 1024, saveDirectory);
			HttpFileUpload fileUpload = new HttpFileUpload(factory);
			List fileItems = fileUpload
					.parseRequest(new HttpUploadContext((HttpEntityEnclosingRequest) request));
			for (FileItem fileItem : fileItems) {
				if (!fileItem.isFormField()) {
					String fileName = fileItem.getName();
			
					if (fileName.contains("/") || fileName.startsWith("image.")) {
						fileName = FileUtils.getCurrentTimes() + ".jpg";
					}
					File uploadedFile = new File(saveDirectory, "/" + fileItem.getFieldName() + "/" + fileName);

					if (uploadedFile.exists()) {
						continue;
					}
					LogUtils.d("ImagePath:" + uploadedFile.getPath());
					fileItem.write(uploadedFile);
				}
			}



你可能感兴趣的:(android应用基于局域网部署网页---上传图片)