brief introduction
Why do I suddenly want to do this, or is it because the memory of the mobile phone is a little small, and it is not very convenient to use the computer to read the learning materials on the computer directly, or do I want to use the mobile phone directly, which gave birth to this idea. After all, as a new youth in the new era, I still love learning
Realization idea
This implementation is relatively simple. It does not need the use of databases and other aspects. It mainly uses the basic java file operation
View all video files in a fixed folder
Here, you can directly use the file operation of java. Directly scan all files under the folder according to the path of the folder and return the names of these files. Later, you need to splice these file names with the mapping address of the access file, so as to read the file
access files
In my impression, it is impossible to access the video file directly through the local path, so a virtual mapping must be set. For example, what I set here is to match file / with the local E:/file/video /, and then splice it with the file name obtained above to access the local file. For example, I want to visit E:/file/video / I graduated again mp4, directly access ip: port number / file / I graduated again mp4 is enough
Important code
Get all video file names
/** * The path here is the actual path of the local video folder * @param path * @return */ public static List getAllFileName(String path) { ArrayList<String> fileNameList = new ArrayList<String>(); boolean flag = false; File file = new File(path); //Get all files in the folder File[] tempList = file.listFiles(); //Add the file name to the list for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { fileNameList.add(tempList[i].getName()); } } return fileNameList; }
Set file virtual path mapping
The reason for this setting is to use the virtual path mapping of springboot to access local video files
Here, use the yml file to set the mapping path
xiaow: video: upload: E:/file/video mapping: /file
Then you need to configure config
@Configuration public class AppConfig extends WebMvcConfigurerAdapter { @Value("${xiaow.video.upload}") private String uploadUrl; @Value("${xiaow.video.mapping}") private String mappingUrl; /** * Here, configure the virtual mapping, that is, we access file / * *, but the actually accessed resource is E:/file/video / * *, so as to access the local file * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(mappingUrl+"/**").addResourceLocations("file:"+ uploadUrl + File.separator); super.addResourceHandlers(registry); } }
The controller layer transfers file data
The main function here is to return the names of all videos in the folder where the videos are stored, so as to facilitate access to these videos
@RestController @RequestMapping("/file") public class FileController { /** * Gets the names of all video files used to access local files * @param path * @return */ @GetMapping("/getFiles") public List<String> getFiles(String path){ return ReadFileUtils.getAllFileName(path); } }
front end
The main front-end is ajax to access the interface, so as to realize the display of files. The blogger's front-end is relatively pull, so they don't make a fool of themselves. The big guys can write a very wow front-end by themselves
Mobile phone to access computer resources
- The key here is that the mobile phone and computer should be under the same LAN, so as long as two devices are under one wifi. Of course, if you have a server, there is no such restriction when deploying to the server
- The url to access the resource is the intranet ip: port number / videolist HTML this way, view your computer's intranet ip and directly open the terminal input
ipconfig
You can view it, as shown below
Then you can access it directly. For example, the blogger's url is like this
http://192.168.0.105:8001/videolist.html
summary
Finally, you can access it with your mobile phone
But the front-end is a little pulled. Let's take the initiative to ignore it
This is the case. If you can get your brothers to do it yourself, you can take the official account below to get the source code and reply to the small video website.