Application background: Recently, the company wants to realize the function of browsing user photos on the TV side. Before, by plugging in the U disk/SD card, the pictures were stored in the user folder in advance. Later, I felt that this method was very cumbersome, and it was necessary to distinguish which user's folder (album) was. So use the application to deploy the webpage and upload the picture by scanning the QR code
TV side:
Mobile page:
Deploying web pages utilizes AndServer, an Android-side web server
Framework github address: https://github.com/yanzhenjie/AndServer
Web UI address: https://github.com/Tencent/weui
Put the uploaded webpage into the project assets directory
Start the service and deploy the web page
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();server save image
FileItemFactory factory = new DiskFileItemFactory(1024 * 1024, saveDirectory); HttpFileUpload fileUpload = new HttpFileUpload(factory); List<FileItem> 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); } }